By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
460,029 Members | 1,336 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 460,029 IT Pros & Developers. It's quick & easy.

Mutually Exclusive CHeckboxes in a GridView

P: 83
Hii experts,

I 've a gridview with one template coloumn with checkbox in that,which is used to to select the row..so once the user select one perticular checkbox..it has to clear the checkbox already in checked state ..i.e. at a time hes allowed to select only one checkbox.(checkboxes should be mutually exclusive)

Requesting you all kindly tell me how to implement this .

Thanks in advance..

Rgds,
BTR.
Jan 28 '09 #1
Share this Question
Share on Google+
9 Replies


Dormilich
Expert Mod 5K+
P: 8,639
better use a radio button, there is only one allowed for selection (given they have the same name attribute)
Jan 28 '09 #2

P: 83
Hii Thanks for your reply....but i would like to provide an option like user should be able to deselect the selected check box thts the reason why i choose checkboxes.

Kincly provide me the solution.

Rgds,
BTR.
Jan 28 '09 #3

acoder
Expert Mod 15k+
P: 16,027
How about a "None of the above" option?
Jan 28 '09 #4

P: 83
if the user wants to modify the details of any row in the gridview he will select the checkbox in the perticular row and cilck the "edit" button.

so i wanna to allow the user to select only one row for editing at a time .This is what my requirment is.
Jan 28 '09 #5

Dormilich
Expert Mod 5K+
P: 8,639
like acoder said, if you want to deselect all radio buttons, make a click button that will do that (call it maybe "unselect all"), that's far easier that trying to force the checkbuttons.
Jan 28 '09 #6

acoder
Expert Mod 15k+
P: 16,027
You could allow them to edit more than one row at any one time.

Anyway, to answer your question, one way is to set all their checked properties to false. Then set the clicked one to true. The alternative proposed by Dormi makes more sense though.
Jan 28 '09 #7

P: 83
Thank you very much for your replies.

I solved it by using javascript.

Expand|Select|Wrap|Line Numbers
  1. function uncheckOthers(id)
  2.     {        
  3.         var elm = document.getElementsByTagName('input');        
  4.         for(var i = 0; i < elm.length; i++)
  5.         {                            
  6.             if(elm.item(i).id.substring(id.id.lastIndexOf('_')) == id.id.substring(id.id.lastIndexOf('_')))
  7.             {
  8.                 if( elm.item(i).type == "checkbox" && elm.item(i)!=id)
  9.                     elm.item(i).checked = false;
  10.             }
  11.         }
  12.     } 
and in the codebehind
Expand|Select|Wrap|Line Numbers
  1. protected void GridViewParticipants_OnRowDataBound(object sender, GridViewRowEventArgs e)
  2. {
  3. if (e.Row.RowType == DataControlRowType.DataRow)
  4.  {
  5.             string strScript = "uncheckOthers(" + ((CheckBox)e.Row.Cells[0].FindControl("chkselect")).ClientID + ");";
  6.             ((CheckBox)e.Row.Cells[0].FindControl("chkselect")).Attributes.Add("onclick", strScript);                 
  7.  
  8.   }
  9. }
Rgds,
BTR.
Jan 28 '09 #8

acoder
Expert Mod 15k+
P: 16,027
Yes, that would be the idea if you insist. However, you could improve it by using the checkbox objects, e.g. document.forms[formName].elements["chkselect"], then you'd only need to check that the ID is not the same.

PS. please use [code] tags when posting code.
Jan 28 '09 #9

P: 1
hey...btreddy
Thats a very good idea.. i was really struggling with the same problem..
Thanks a lot buddy..
Feb 5 '10 #10

Post your reply

Sign in to post your reply or Sign up for a free account.