I have the following checkBox function in a gridView-
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:CheckBox ID="chkRow" runat="server" OnCheckedChanged="CheckBoxPN_CheckedChanged" AutoPostBack="true" />
</ItemTemplate>
Which creates a checkBox for every row created in the gridView.
My problem being that in the event-
protected void CheckBoxPN_CheckedChanged(Object sender, EventArgs e)
{
CheckBox chk = (CheckBox)sender;
GridViewRow row = (GridViewRow)chk.NamingContainer;
int gID = System.Convert.ToInt16(row.Cells[1].Text);
gridViewDataSource.SelectCommand = sqlCommand + gID;
}
I am using this to retrieve values. How ever once a user checks a checkBox, then goes to check another one, the previous checkBox state is still checked.
How can I modify the code to uncheck the last checkBox when the new one is checked?
EDIT
@Neil Knight ‘s answer implements the correct functionality, however as I am using the checked_changed event, when the new checkBox is getting checked, this fires the event, and gets the correct answer. However when the previous checkBox gets unchecked, this again fires the event and gets the wrong answer. How can I stop the gID value getting replaced with the old answer?
ASP.Net example:
Taken from Stephen Cheng‘s answer.