管理者が更新するためにこれらのGridViewを設定する必要があります。だから私は 多くの従業員と各GridViewのコースの多くを持っているので、私は最も良いと思う GridViewを更新する方法は、空欄をチェックボックスとして表示すると、 管理者は、従業員、彼が何をするのか は、チェックボックスをチェックして、[更新]ボタンをクリックするだけです。 空のフィールドをチェックボックスとして表示できるようになりましたが、現在はデータベースにチェックボックスをチェックする値を投稿する方法を に通知する方法がわかりません。私はそれを探していますチェックボックスをオンにして[保存]ボタンをクリックしてGridViewを更新するにはどうすればよいですか?
シナリオは、それはすでに前にチェックされている場合、システムは、それぞれのGridView内のすべての チェックボックスにチェックを作ること があるとして、それを残すことです。それが空で今すぐチェックされている場合は、従業員のレコードの データベースに追加する必要があります。したがって、保存ボタンをクリックするとGridViewが更新されます。私の元のコードは次のとおりです。
<asp:Repeater ID="Repeater1" runat="server" DataSourceID="SqlDataSource1">
<ItemTemplate>
<asp:HiddenField ID="HiddenField1" runat="server" Value='<%# Eval("GroupID")%>' />
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:testConnectionString %>"
SelectCommandType="StoredProcedure" SelectCommand="kbiReport">
<%--FilterExpression="[Division] like '{0}%' and [Organization] like '{1}%'">--%>
<%--<FilterParameters>
<asp:ControlParameter ControlID="ddlDivision" Name="Division"
PropertyName="SelectedValue" Type="String" />
<asp:ControlParameter ControlID="ddlOrganization" Name="Organization"
PropertyName="SelectedValue" Type="String" />
</FilterParameters>--%>
<SelectParameters>
<%--ControlParameter is linked to the HiddenField above to generate different GridView based on different values
of GroupID--%>
<asp:ControlParameter ControlID="HiddenField1" Name="GroupID" PropertyName="Value" />
</SelectParameters>
</asp:SqlDataSource>
<asp:GridView ID="GridView1" runat="server"
AllowSorting="True"
CellPadding="3"
DataSourceID="SqlDataSource1"
CssClass="mGrid"
AlternatingRowStyle-CssClass="alt"
RowStyle-HorizontalAlign="Center"
OnRowDataBound="GridView1_RowDataBound" OnDataBound="GridView1_DataBound">
<AlternatingRowStyle BackColor="White" ForeColor="#284775" />
<HeaderStyle Font-Bold = "true" ForeColor="Black"/>
<Columns>
<asp:CommandField ShowSelectButton="True" />
<%--<asp:TemplateField HeaderText="Select">
<ItemTemplate>
<asp:CheckBox ID="chkSelect" runat="server" AutoPostBack="true" OnCheckedChanged="chkSelect_CheckedChanged"/>
</ItemTemplate>
</asp:TemplateField>--%>
</Columns>
<EditRowStyle BackColor="#999999" />
<FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
<RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
<SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
<SortedAscendingCellStyle BackColor="#E9E7E2" />
<SortedAscendingHeaderStyle BackColor="#506C8C" />
<SortedDescendingCellStyle BackColor="#FFFDF8" />
<SortedDescendingHeaderStyle BackColor="#6F8DAE" />
</asp:GridView>
</ItemTemplate>
</asp:Repeater>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:testConnectionString %>"
SelectCommand="SELECT DISTINCT GroupID FROM courses">
</asp:SqlDataSource>
<asp:Button ID="btnSave" runat="server" OnClick="btnSave_Click" Text="Save" />
<br /><br />
コードビハインド:
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
foreach (TableCell c in e.Row.Cells)
{
// Check if the cell vlaue = Yes
// if it is Yes, the cell will be colored with Light Green
if (c.Text.Equals("Yes"))
{
c.BackColor = System.Drawing.Color.LightGreen;
}
}
}
// The following is for changing the color of headers in each GridView based on the value of the HiddenFild
// BTW, the value of the HiddenField is the value of the GroupID in Group Table in the Database
else if(e.Row.RowType == DataControlRowType.Header){
switch (((HiddenField)((GridView)sender).Parent.FindControl("HiddenField1")).Value)
{
case "1":
for (int i = 5; i < e.Row.Cells.Count; i++)
e.Row.Cells[i].BackColor = System.Drawing.Color.LightBlue;
break;
case "2":
for (int i = 5; i < e.Row.Cells.Count; i++)
e.Row.Cells[i].BackColor = System.Drawing.Color.LightYellow;
break;
case "3":
for (int i = 5; i < e.Row.Cells.Count; i++)
e.Row.Cells[i].BackColor = System.Drawing.Color.Orange;
break;
}
}}
//For inserting checkboxes inside all courses buttons
protected void GridView1_DataBound(object sender, EventArgs e){
GridView GridView1 = (GridView)sender;
foreach (GridViewRow objRow in GridView1.Rows)
{
for (int i = 5; i < objRow.Cells.Count; i++){
CheckBox chkCheckBox = new CheckBox();
objRow.Cells[i].Controls.Add(chkCheckBox);
if (objRow.Cells[i].BackColor == System.Drawing.Color.LightGreen)
chkCheckBox.Checked = true;
}
}
}
//For updating the GridView (Save Button)
protected void btnSave_Click(object sender, EventArgs e)
{
}
UPDATE:私は、次のようにbtnSave_Clickボタンを修正 :
//For updating the GridView (Save Button)
protected void btnSave_Click(object sender, EventArgs e)
{
GridView GridView1 = (GridView)sender;
// Are there checked boxes?
List<int> CheckBoxList = new List<int>();
for (int i = 0; i < GridView1.Rows.Count; i++)
{
int courseid = (int)GridView1.DataKeys[i][0];
CheckBox checkBox = (CheckBox)GridView1.Rows[i].FindControl("CheckBox");
if (checkBox.Checked)
{
CheckBoxList.Add(courseid);
}
}
}
をしかし、私が得ました次のエラー: Sys.WebForms.PageRequestManagerServerErrorException:型のオブジェクトをキャストすることができません'System.Web.UI.WebControls.Button'と入力して 'System.Web.UI.WebControls.GridView'と入力します。
このエラーが発生した理由はわかりません。誰も私にこれを手伝ってもらえますか?
ご協力ありがとうございますが、エラーが発生しました。Sys.WebForms.PageRequestManagerServerErrorException:インデックスが範囲外です。負でなく、コレクションのサイズより小さくなければなりません。パラメータ名:indexこのエラーも私には分かりません。この問題で私を助けてくれますか? –
@ MohammedAl-Ali - 私は自分の投稿にコードをテストしていません。私はあなたが投稿したコードに基づいてあなたを提案することができます。 – adatapost