1

I am using checkbox in the asp.net gridview. I want select only one checkbox at time. if I select one checkbox other checkboxes should be deselected.

I have html view source

<input id="ctl00_MainContent_mGrid_ob_mGridBodyContainer_ctl02_ctl02_ctl00_ChkID" type="checkbox" 
<input id="ctl00_MainContent_mGrid_ob_mGridBodyContainer_ctl03_ctl02_ctl00_ChkID" type="checkbox"
2
  • 7
    Maybe you want to use radio button instead? Commented Jun 20, 2012 at 22:34
  • Do you have 2 checkboxes by row? I agree that a radiobutton is better for this case though. Commented Jun 20, 2012 at 22:35

3 Answers 3

4

Usually radio buttons are used for the exclusive functionality where only one item in a group is selected and the browser will do taht for you automatically with the right HTML.

For checkboxes, you could code it with jQuery like this:

<div class="checkboxContainer">
    <input id="ctl00_MainContent_mGrid_ob_mGridBodyContainer_ctl02_ctl02_ctl00_ChkID" type="checkbox"> Item 1<br>
    <input id="ctl00_MainContent_mGrid_ob_mGridBodyContainer_ctl03_ctl02_ctl00_ChkID" type="checkbox"> Item 2<br>
    <input id="ctl00_MainContent_mGrid_ob_mGridBodyContainer_ctl04_ctl02_ctl00_ChkID" type="checkbox"> Item 3
</div>​

$("input[type='checkbox']").change(function() {
    $(this).closest(".checkboxContainer").find("input[type='checkbox']").not(this).prop("checked", false);
    $(this).prop("checked", true);
});

​ Working Demo: http://jsfiddle.net/jfriend00/hWEXx/

Here's the pure HTML way of doing exclusive radio buttons (also in that same demo):

<div class="radioGroup">
    <input type="radio" name="group1">Item A<br>
    <input type="radio" name="group1">Item B<br>
    <input type="radio" name="group1">Item C<br>
</div>
Sign up to request clarification or add additional context in comments.

Comments

3

Use a radio button instead of a checkbox.

<form>
    <input type="radio" name="parents" value="Mom" /> Mom<br />
    <input type="radio" name="parents" value="Dad" /> Dad
</form>

2 Comments

This is indeed the kind of situation radio buttons were made for. Besides the extra work required to make checkboxes act like you want, radio buttons are more semantically correct -- their very existence is a visual clue that "hey, you can only pick one of these" -- and they'll do the right thing even if for some reason JavaScript is disabled.
Besides radio buttons being the natural fit for the job. You bring up a great point in regard to the possibility of JavaScript being disabled.
0

in asp.net you should use radiobuttons instead checkboxes (in this case)

<div class="group">
   <asp:RadioButton Id="radio1" runat="server" GroupName="radioGroup" />
   <asp:RadioButton Id="radio2" runat="server" GroupName="radioGroup" />
</div>

GroupName attribute make this functionality you need. (If radio1 is Checked the radio2 is automatically unchecked. hope this help:)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.