Click here to Skip to main content
14,931,069 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
I need to validate a gridview (ie. is empty or not)
(Gridview data will be filled by user through datatable.)

In my code it is not able to find the Gridview1.
and also advise my code / way is correct..
//=======================================================
XML
<script  language="javascript" type ="text/javascript" >
     function gvPostDatedChequeValidation(source, args)
       {
        var Grid = document.getElementById("<%=GridView1.ClientID%>");
        if (Grid.Rows.Count>0 )
         {
         args.IsValid = true;
         }
         else
         {
         args.IsValid = false;
         }
       }
       </script>

//=============================================================
XML
<asp:GridView ID="GridView1" runat="server">
  </asp:GridView>

  <asp:CustomValidator ID="CustomValidator2" runat="server"  ErrorMessage="Fill Blank Cheque Details"
  ClientValidationFunction="gvPostDatedChequeValidation">*</asp:CustomValidator>

  <asp:Button ID="Button1" runat="server" Text="Button" />
Posted
Updated 17-Jan-21 4:55am

1 solution

Count property is not valid in javascript
use length property like this

SQL
if (Grid.rows.length>0 )
        {
}
   
Comments
SHAJANCHERIAN 2-May-11 2:44am
   
Thank You Ashishmau1.
But still there is a problem . ie.
Always getting null in var Grid. My code is given below. Is there any mistake.
var Grid = document.getElementById("<%=GridView1.ClientID%>");
Ashishmau 2-May-11 2:55am
   
first of all dont use variable Grid as it might predefined.
use any other custom variable and try.
SHAJANCHERIAN 2-May-11 3:16am
   
Intitialy as there was no rows in the Gridview1, Javascript was unable to find the Gridview.
So I have corrected my code as follows. Now it is working.
//======================================================
function gvPostDatedChequeValidation(source, args)
{
var Grid = document.getElementById("<%=gvPostDatedCheque.ClientID%>");
if (Grid==null )
{
args.IsValid = false;

}
else
{
args.IsValid = true;
}
}

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900