Skip to content

Client-Side Validation for the CheckBoxes Inside a GridView

November 24, 2014
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False">
      <asp:BoundField HeaderText="n" DataField="sno"> 
       <HeaderStyle HorizontalAlign="Center" VerticalAlign="Middle" Width="50px" />
          <ItemStyle HorizontalAlign="Center" VerticalAlign="Middle" />
       <asp:TemplateField HeaderText="Select">
               <asp:CheckBox ID="chkBxSelect" runat="server" />
            <HeaderStyle HorizontalAlign="Center" VerticalAlign="Middle" />
            <ItemStyle HorizontalAlign="Center" VerticalAlign="Middle" />
 <asp:Button ID="btnPost" runat="server" Text="Post" 
             OnClientClick="javascript:return TestCheckBox();"
             OnClick="btnPost_Click" />

javascript code:
<script type="text/javascript">
   var TargetBaseControl = null;
   window.onload = function()
         //get target base control.
         TargetBaseControl = 
           document.getElementById('<%= this.GridView1.ClientID %>');
         TargetBaseControl = null;
   function TestCheckBox()
      if(TargetBaseControl == null) return false;
      //get target child control.
      var TargetChildControl = "chkBxSelect";
      //get all the control of the type INPUT in the base control.
      var Inputs = TargetBaseControl.getElementsByTagName("input"); 
      for(var n = 0; n < Inputs.length; ++n)
         if(Inputs[n].type == 'checkbox' && 
            Inputs[n].id.indexOf(TargetChildControl,0) >= 0 && 
          return true;        
      alert('Select at least one checkbox!');
      return false;


From → Uncategorized

Leave a Comment

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: