How To Select One Check Box From A Group Of Checkboxes

Kasarlaravi
Posted by in JavaScript category on for Intermediate level | Points: 250 | Views : 28035 red flag
Rating: 5 out of 5  
 1 vote(s)

We can access the check box in grid control in client side using java script

Introduction

In This article we shall learn how to restrict user to select multiple recipients.

Using the code

We can access the check box in grid control in client side using java script.

When there are two recipients groups available to user to send messages to them .for Group 1 user will be allow to send one recipient at a time , for Group 2  he will  be allow to send multiple. For 2nd case there is no need to write any JavaScript code because check boxes allow us select multiple at a time.


 

For Group 1 we need to write below java script code:

<script type="text/javascript" language="javascript">

function checkboxClicked(event , idfrag)  

        {  

            var currentCheckBox = event.srcElement || event.target;        

            var inputs = document.getElementsByTagName("input");  

            for (var i = 0; i < inputs.length; i++)  

            {  

                var input = inputs[i];            

                if (input.id == currentCheckBox.id)  

                    continue

                if (input.id.indexOf(idfrag) < 0)  

                    continue;               

                //clear out the rest of the checkboxes  

                if (input.type && input.type == "checkbox")  

                {  

                    input.checked = false;  

                }  

            }  

        }

</script>

 

.ASPX file::

<asp:GridView ID="grdRecipients" runat="server"   DataKeyNames="id"

                  AutoGenerateColumns="False" CellPadding="7" GridLines="None"  Width="100%" EmptyDataText="No Recipients Available" HeaderStyle-BackColor="White"  BackColor="#FBF9FC"  AlternatingRowStyle-BackColor="#F0EAF6"  >

<RowStyle />

<Columns>

    <asp:TemplateField HeaderText="">

       <ItemTemplate>

          <asp:CheckBox ID="chkAssign" runat="server"  ToolTip='<%# Eval("id") %>' AutoPostBack="true"onclick="checkboxClicked(event,'chkAssign')"  />

        </ItemTemplate>

       <ItemStyle Width="25%" />

      </asp:TemplateField>

   </Columns>

 <FooterStyle CssClass="display" />

      <HeaderStyle BackColor="White" CssClass="display" />

 </asp:GridView>

Conclusion

Single Check box selection from a group of check boxes is a very rare condition. If any other solution easier then this please respond to this article.

Page copy protected against web site content infringement by Copyscape

About the Author

Kasarlaravi
Full Name: ravi kasarla
Member Level: Starter
Member Status: Member
Member Since: 6/30/2011 7:08:03 AM
Country: India
Ravi Kasarla
http://www.dotnetfunda.com
HI , i am currently working for oztek software pvt ltd as a web developer.i am having 1.5 years experience on .net frame work and php.

Login to vote for this post.

Comments or Responses

Login to post response

Comment using Facebook(Author doesn't get notification)