I have created an application aps.net c# within VS2019
I have a grid view, which has a checkbox in the column header.
Once a user has clicked on a seperate button to display, I would like to validate the checkbox called "chkALL" thats within the column header to see if it is checked or not. I cant seems to get the right syntax to do this.
Any advise please?
My ASPX code is here:
<asp:TemplateField HeaderText="Chg">
<asp:CheckBox ID="ChkAll" runat="server" AutoPostBack="true" OnCheckedChanged="ChkAll_CheckedChanged" />
<asp:CheckBox ID="ChkRow" runat="server" AutoPostBack="true" OnCheckedChanged="ChkRow_CheckedChanged" RowIndex="<%# Container.DataItemIndex %>" />
</asp:TemplateField>
This is the c# code I have tried
CheckBox chkbox = (CheckBox)tgvCorrelations.Column[13].FindControl("chkAll");
2
Answers
To get/use and enjoy the value of the check box in the heading?
You can use find control against the GridView.HeaderRow.
So, assuming this markup:
So, when we hit the button to load the GridView, we then will get the value of the check box in the heading, and set all rows to checked (or un-checked).
So, the code to load our grid looks like this:
Note VERY close, that WHEN we re-bind the GridView, then the setting of the check box is lost. So, if we are to re-bind the GridView, but ALSO respect the check box, then we get/grab/save the check box, re-load grid, and then re-set the check box back into the heading. (the value is LOST when we re-bind, so above code takes this into account).
Note that after the grid loads, one might still want to check all (or un-check all) by clicking on the header. That code is somewhat easier since we can use "sender" to get the check box in question.
So, that code is:
And the result is thus this: