Click here to Skip to main content
11,496,146 members (343 online)
The site is currently in read-only mode for maintenance. Posting of new items will be available again shortly.
See more: Javascript VB.NET
function GridVal() 
{
    var app_id = document.getElementById ('ctl00_contentMain_ddl_Approach').value;
                 if (app_id =="1008") 
                 {      
                   var cellInstr;
                   var cellInstr1;
                   var grid = document.getElementById("<%= QueGrid.ClientID %>");
                   var txt1 = document.getElementById("txt_QuestionAssessmentInstruction");  
                     for (i=1; i<=grid.rows.length; i++)
                        { 
                          alert("for");   
                          alert(i);                     
                          cellInstr = grid.rows[i].cells[4];                                 
                          alert(cellInstr);                              
                          var CInstruc = cellInstr.childNodes[0].value;
                              alert(CInstruc); 
                         if(CInstruc=="")
                         {
                           alert("if");   
                           alert("The field Question Instruction cannot be Empty");
                           cellInstr.childNodes[0].focus();
                           return false;
                         }
                       }
                 }
                 else
                 {
                   var cellInstr;                
                   var grid = document.getElementById("<%= BUGrid.ClientID %>");
                   var txt1 = document.getElementById("txtControlTI");       
                     for (i=0; i<grid.rows.length;>
                        {                              
                          cellInstr = grid.rows[i].cells[6];
                          alert(cellInstr);                             
                          var CInstruc = cellInstr.childNodes[0].value;                       
                             alert(CInstruc);
                         if(CInstruc=="")
                         {
                            alert("The field Control Instruction cannot be Empty");
                            cellInstr.childNodes[0].focus();
                            return false;
                         }                        
                       }
                      }
}
In my above code i have used QueGrid as datagrid i want to give alert msg for number of rows in datagrid how can i get number of rows for the grid
Posted 27-Jun-13 22:25pm
Edited 27-Jun-13 22:34pm
v3
Refer - ASP.NET GridView row count using Javascript[^].
var rowscount = document.getElementByID('<%=Gridview1.ClientID%>').rows.length;
 
alert(rowscount);
Comments
Member 9410081 at 28-Jun-13 5:12am
   
i tried the other one stilll its not working
Tadit Dash at 28-Jun-13 5:46am
   
Then put a debugger and see what is the problem. You can put that by adding the code debugger; inside javaScript code.

And yes, please see the Console Window of FireBug in FireFox, if there are any issues reported.
var grid = document.getElementById("<%=QueGrid.ClientID %>");
var rowCount = grid.getElementsByTagName('TR').length -1;
  Permalink  
Comments
Member 9410081 at 28-Jun-13 4:59am
   
i used the above code but it is giving following error.

Message: 'null' is null or not an object
Line: 160
Char: 1
Code: 0
Mahesh Bailwal at 28-Jun-13 5:49am
   
Where are you using this script in Head section or in Body section of .aspx page
Mahesh Bailwal at 28-Jun-13 6:05am
   
It seems like you trying to get row count before grid is fully loaded. Please add this script just before end of body tag in .aspx page.
Try this ....Smile | :)

DataTable dt = //configure datasource here 
GridView1.DataSource = dt;
GridView1.DataBind();
 
you can take a hiddenfield html control in your page and can assign its value when you bind your grid as
 
HiddenField1.value = GridView1.Rows.Count.ToString(); // HiddenField1 is your hiddent type with server tag

 

In your javascript you can get its value as
 
var count = document.getElementById('HiddenField1');
 
alert(count.value)
  Permalink  

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

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 10,401
1 OriginalGriff 8,910
2 Sascha Lefèvre 3,899
3 Maciej Los 3,422
4 Richard Deeming 2,600


Advertise | Privacy | Mobile
Web02 | 2.8.150520.1 | Last Updated 28 Jun 2013
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100