Mutually Exclusive CHeckboxes in a GridView

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • btreddy
    New Member
    • Oct 2008
    • 83

    Mutually Exclusive CHeckboxes in a GridView

    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.(check boxes should be mutually exclusive)

    Requesting you all kindly tell me how to implement this .

    Thanks in advance..

    Rgds,
    BTR.
  • Dormilich
    Recognized Expert Expert
    • Aug 2008
    • 8694

    #2
    better use a radio button, there is only one allowed for selection (given they have the same name attribute)

    Comment

    • btreddy
      New Member
      • Oct 2008
      • 83

      #3
      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.

      Comment

      • acoder
        Recognized Expert MVP
        • Nov 2006
        • 16032

        #4
        How about a "None of the above" option?

        Comment

        • btreddy
          New Member
          • Oct 2008
          • 83

          #5
          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.

          Comment

          • Dormilich
            Recognized Expert Expert
            • Aug 2008
            • 8694

            #6
            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.

            Comment

            • acoder
              Recognized Expert MVP
              • Nov 2006
              • 16032

              #7
              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.

              Comment

              • btreddy
                New Member
                • Oct 2008
                • 83

                #8
                Thank you very much for your replies.

                I solved it by using javascript.

                Code:
                function uncheckOthers(id)
                    {        
                        var elm = document.getElementsByTagName('input');        
                        for(var i = 0; i < elm.length; i++)
                        {                            
                            if(elm.item(i).id.substring(id.id.lastIndexOf('_')) == id.id.substring(id.id.lastIndexOf('_')))
                            {
                                if( elm.item(i).type == "checkbox" && elm.item(i)!=id)
                                    elm.item(i).checked = false;
                            }
                        }
                    }
                and in the codebehind
                Code:
                protected void GridViewParticipants_OnRowDataBound(object sender, GridViewRowEventArgs e)
                {
                if (e.Row.RowType == DataControlRowType.DataRow)
                 {
                            string strScript = "uncheckOthers(" + ((CheckBox)e.Row.Cells[0].FindControl("chkselect")).ClientID + ");";
                            ((CheckBox)e.Row.Cells[0].FindControl("chkselect")).Attributes.Add("onclick", strScript);                 
                            
                  }
                }
                Rgds,
                BTR.
                Last edited by acoder; Jan 28 '09, 12:08 PM. Reason: Added [code] tags

                Comment

                • acoder
                  Recognized Expert MVP
                  • Nov 2006
                  • 16032

                  #9
                  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.

                  Comment

                  • bipinchacko
                    New Member
                    • Aug 2008
                    • 1

                    #10
                    hey...btreddy
                    Thats a very good idea.. i was really struggling with the same problem..
                    Thanks a lot buddy..

                    Comment

                    Working...