Click here to Skip to main content
15,897,187 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
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
Updated 27-Jun-13 21:34pm
v3

Refer - ASP.NET GridView row count using Javascript[^].
JavaScript
var rowscount = document.getElementByID('<%=Gridview1.ClientID%>').rows.length;

alert(rowscount);
 
Share this answer
 
Comments
Member 9410081 28-Jun-13 5:12am    
i tried the other one stilll its not working
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;
 
Share this answer
 
Comments
Member 9410081 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 28-Jun-13 5:49am    
Where are you using this script in Head section or in Body section of .aspx page
Mahesh Bailwal 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 ....:)

C#
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)
 
Share this answer
 

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