Ebikeneser
Ebikeneser

Reputation: 2364

One checkBox to be checked

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?

Upvotes: 0

Views: 550

Answers (2)

Neil Knight
Neil Knight

Reputation: 48537

ASP.Net example:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<title>checkboxes</title>
<meta content="Microsoft Visual Studio .NET 7.1" name="GENERATOR">
<meta content="Visual Basic .NET 7.1" name="CODE_LANGUAGE">
<meta content="JavaScript" name="vs_defaultClientScript">
<meta content="http://schemas.microsoft.com/intellisense/ie5"
name="vs_targetSchema">
<script language="javascript">
function SingleSelect(regex,current)
{
re = new RegExp(regex);

for(i = 0; i < document.forms[0].elements.length; i++) {

elm = document.forms[0].elements[i];

if (elm.type == 'checkbox') {

if (re.test(elm.name)) {

elm.checked = false;

}
}
}

current.checked = true;

}
</script>
</HEAD>
<body>
<form id="Form1" method="post" runat="server">
<table width="100%" align="center">
<tr>
<td>
<br/>aaaa<input type="checkbox" id="chkOne" name="chkOne"
value="aaaa" checked onclick="SingleSelect('chk',this);" />
<br/>bbbb<input type="checkbox" id="chkTwo" name="chkTwo"
value="bbbb" onclick="SingleSelect('chk',this);" />
<br/>cccc<input type="checkbox" id="chkThree" name="chkThree"
value="cccc" onclick="SingleSelect('chk',this);" />
<br/>dddd<input type="checkbox" id="chkFour" name="chkFour"
value="dddd" onclick="SingleSelect('chk',this);" />
<br/>eeee<input type="checkbox" id="chkFive" name="chkFive"
value="eeee" onclick="SingleSelect('chk',this);" />
</td>
</tr>
<tr>
<td></td>
</tr>
<tr>
<td></td>
</tr>
</table>
</form>
</body>
</HTML>

Taken from Stephen Cheng's answer.

Upvotes: 2

Aniket Inge
Aniket Inge

Reputation: 25695

You can use a radio button as suggested by X.L.Ant (do remember to make them into the same group(you do this with GroupName="FOO" property of radio buttons))

alternatively(if Checkbox is what you really want to use)

you can unset all the CheckBoxes, except the one selected(which is terribly slow).

<ItemTemplate>
    <asp:RadioButton ID="chkRow" runat="server" OnCheckedChanged="CheckBoxPN_CheckedChanged" AutoPostBack="true" GroupName="foo"/>
 </ItemTemplate>

Upvotes: 0

Related Questions