Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to update the GridView after checking the checkbox and clicking the save button?
    text
    copied!<p>I need to set these GridViews for updating by the admin. So since I have a lot of employees and a lot of courses in each GridView, I think the best way to update the GridView is to show the blank fields as checkboxes and when the Admin wants to update the record of one of the employees, all what he needs to do is just checking the checkboxes and click update button. <b>I could be able to show the empty fields as checkboxes but now I don't know how to post the value of checking the checkbox to the database.</b></p> <p>The scenario that I am looking for it is to make the system check every checkbox in each GridView, if it is already checked before, leave it as it is. If it was empty and it is just checked now, it should be added to the database in the record of the employee. Therefore, the GridView will be updated with clicking the save button. My original code is:</p> <pre><code>&lt;asp:Repeater ID="Repeater1" runat="server" DataSourceID="SqlDataSource1"&gt; &lt;ItemTemplate&gt; &lt;asp:HiddenField ID="HiddenField1" runat="server" Value='&lt;%# Eval("GroupID")%&gt;' /&gt; &lt;asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="&lt;%$ ConnectionStrings:testConnectionString %&gt;" SelectCommandType="StoredProcedure" SelectCommand="kbiReport"&gt; &lt;%--FilterExpression="[Division] like '{0}%' and [Organization] like '{1}%'"&gt;--%&gt; &lt;%--&lt;FilterParameters&gt; &lt;asp:ControlParameter ControlID="ddlDivision" Name="Division" PropertyName="SelectedValue" Type="String" /&gt; &lt;asp:ControlParameter ControlID="ddlOrganization" Name="Organization" PropertyName="SelectedValue" Type="String" /&gt; &lt;/FilterParameters&gt;--%&gt; &lt;SelectParameters&gt; &lt;%--ControlParameter is linked to the HiddenField above to generate different GridView based on different values of GroupID--%&gt; &lt;asp:ControlParameter ControlID="HiddenField1" Name="GroupID" PropertyName="Value" /&gt; &lt;/SelectParameters&gt; &lt;/asp:SqlDataSource&gt; &lt;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"&gt; &lt;AlternatingRowStyle BackColor="White" ForeColor="#284775" /&gt; &lt;HeaderStyle Font-Bold = "true" ForeColor="Black"/&gt; &lt;Columns&gt; &lt;asp:CommandField ShowSelectButton="True" /&gt; &lt;%--&lt;asp:TemplateField HeaderText="Select"&gt; &lt;ItemTemplate&gt; &lt;asp:CheckBox ID="chkSelect" runat="server" AutoPostBack="true" OnCheckedChanged="chkSelect_CheckedChanged"/&gt; &lt;/ItemTemplate&gt; &lt;/asp:TemplateField&gt;--%&gt; &lt;/Columns&gt; &lt;EditRowStyle BackColor="#999999" /&gt; &lt;FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" /&gt; &lt;PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" /&gt; &lt;RowStyle BackColor="#F7F6F3" ForeColor="#333333" /&gt; &lt;SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" /&gt; &lt;SortedAscendingCellStyle BackColor="#E9E7E2" /&gt; &lt;SortedAscendingHeaderStyle BackColor="#506C8C" /&gt; &lt;SortedDescendingCellStyle BackColor="#FFFDF8" /&gt; &lt;SortedDescendingHeaderStyle BackColor="#6F8DAE" /&gt; &lt;/asp:GridView&gt; &lt;/ItemTemplate&gt; &lt;/asp:Repeater&gt; &lt;asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="&lt;%$ ConnectionStrings:testConnectionString %&gt;" SelectCommand="SELECT DISTINCT GroupID FROM courses"&gt; &lt;/asp:SqlDataSource&gt; &lt;asp:Button ID="btnSave" runat="server" OnClick="btnSave_Click" Text="Save" /&gt; &lt;br /&gt;&lt;br /&gt; </code></pre> <p>The Code-Behind:</p> <pre><code>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 &amp;lt; e.Row.Cells.Count; i++) e.Row.Cells[i].BackColor = System.Drawing.Color.LightBlue; break; case "2": for (int i = 5; i &amp;lt; e.Row.Cells.Count; i++) e.Row.Cells[i].BackColor = System.Drawing.Color.LightYellow; break; case "3": for (int i = 5; i &amp;lt; 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 &amp;lt; 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) { } </code></pre> <p><strong>UPDATE:</strong> I modified the btnSave_Click button to be as following:</p> <pre><code>//For updating the GridView (Save Button) protected void btnSave_Click(object sender, EventArgs e) { GridView GridView1 = (GridView)sender; // Are there checked boxes? List&lt;int&gt; CheckBoxList = new List&lt;int&gt;(); for (int i = 0; i &lt; 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); } } } </code></pre> <p>But I got the following error: <strong>Sys.WebForms.PageRequestManagerServerErrorException: Unable to cast object of type 'System.Web.UI.WebControls.Button' to type 'System.Web.UI.WebControls.GridView'</strong></p> <p>I don't know why I got this error. Could anyone help me with this?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload