Validating controls of Editable GridView using Javascript

Posted: April 17, 2011 in ASP.Net

When you have a GridView with editable controls like TextBox,CheckBox etc it becomes necessary that you validate the controls before clicking update.I am sure there could be different ways to access the controls in GridView.Here I am posting  how it is done without hitting the server side using javascript.

<EditItemTemplate> <asp:LinkButton ID="lbkUpdate" runat="server"
 OnClientClick='javascript:return Validategrid(this);' CausesValidation="True" CommandName="Update" Text="Update"></asp:LinkButton> </EditItemTemplate>
 function Validategrid(btnCtrl)
var gridView = document.getElementById('<%= grd1.ClientID %>');
var selectedRowIndex = btnCtrl.parentNode.parentNode.rowIndex;
var txt1 = gridView.rows[parseInt(selectedRowIndex)].cells[0].children[0];
var txt2 = gridView.rows[parseInt(selectedRowIndex)].cells[1].children[0];
var chk1 = gridView.rows[parseInt(selectedRowIndex)].cells[2].children[0]; ;
var chk2= gridView.rows[parseInt(selectedRowIndex)].cells[3].children[0];

 if (chk1.checked == false && txt1.value == "" && chk2.checked == false && txt1.value == "")
   alert("Please provide inputs");
   return false;
  1. devendra Patidar says:

    its great ! which helps me validate text box input in Grid-view.

  2. mahim says:

    Thnx a lot 🙂

  3. Suresh Kumar says:


  4. abhinav says:

    thanx a lot

  5. Sayeed Ahmed says:

    very nice…code

  6. lokesh says:

    grt work

  7. Mahadevi says:

    thank you

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