how to check number of rows in a gridview in client sidde

I want to validate of there are atleast 1 row in a gridview using javascript or jquery. The

I tried something like this , but doesn t work. Please let me know.
if (document.getElementById("<%= gdvDept.ClientID %>").get_rows().length == 0) {
            showError($('#divError'), "• Please add dept location floor");
        }

Please help. thanks
TrialUserAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

leakim971PluritechnicianCommented:
var number_of_rows = document.getElementById("<%= gdvDept.ClientID %>").getElementsByTagName("tr").length;
0
TrialUserAuthor Commented:
I get the error:
Microsoft JScript runtime error: Unable to get value of the property 'getElementsByTagName': object is null or undefined
0
leakim971PluritechnicianCommented:
you need to wait the loading of the page (DOM ready)

For testing purpose, confirm the following work :

<script>
window.onload = function() {
   var number_of_rows = document.getElementById("<%= gdvDept.ClientID %>").getElementsByTagName("tr").length;
   alert( number_of_rows );
}
</script>

if not, you don't have an element with gdvDept as ID
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

TrialUserAuthor Commented:
Again I get the error with the test code.
Microsoft JScript runtime error: Unable to get value of the property 'getElementsByTagName': object is null or undefined

I have
<asp:GridView ID="gdvDept" runat="server" AutoGenerateColumns="false"  OnRowCommand = "gdvDept_RowCommand" CellPadding="5" CellSpacing="5">
0
leakim971PluritechnicianCommented:
can you post the generated page? (do a right click in your browser and choose view source)
thanks
0
TrialUserAuthor Commented:
Thakn you very much for working with me on this.
When I viewsource I do not see any element gdvDept. Is it becuase I have an updatepanel ? I have attached the source and the actual html:


Viewsource :
 <div id="MainContent_UpdatePanel2">
      
                    <div style="float:right;">
                     <input type="image" name="ctl00$MainContent$imgBtnAddNew" id="MainContent_imgBtnAddNew" title="Add New Dept Location Floor" src="../images/add2.png" />
                    </div>
                    <center>
                    <div style="margin:10px;" id="divDept">
                         <div>

      </div>    
               </div>          
               </center>
                </div>  

Actual html :
  <asp:UpdatePanel runat="server" ID="UpdatePanel2" UpdateMode="Conditional">
            <Triggers>
                <asp:AsyncPostBackTrigger ControlID="imgBtnAddNew" />
            </Triggers>
            <ContentTemplate>
                    <div style="float:right;">
                     <asp:ImageButton runat="server" ID="imgBtnAddNew" ImageUrl="~/images/add2.png"  
                            ToolTip="Add New Dept Location Floor" onclick="imgBtnAddNew_Click"/>
                    </div>
                    <center>
                    <div style="margin:10px;" id="divDept">
                         <asp:GridView ID="gdvDept" runat="server" AutoGenerateColumns="false"  OnRowCommand = "gdvDept_RowCommand" CellPadding="5" CellSpacing="5">
                         <HeaderStyle ForeColor = "#ffffff"  BackColor="#4C789B" HorizontalAlign="Center" />
               <Columns>
               <asp:BoundField DataField = "AuditMgmt_DeptLocFloor_ID" Visible = "false" />
                <asp:BoundField DataField="Department" HeaderText="Department" />
                <asp:BoundField DataField="DepartmentID" Visible ="false"   HeaderText = "Department"/>
                <asp:BoundField DataField="Location" HeaderText = "Location" />
                <asp:BoundField DataField="LocationID"  Visible ="false" />
                <asp:BoundField DataField="Floor" HeaderText="Floor" />
                <asp:BoundField DataField="FloorID"  Visible ="false"/>
                  <asp:TemplateField HeaderText = "Delete">                                
                                <ItemTemplate>
                                <asp:ImageButton ID="imgDelete" runat="server" ImageUrl="~/images/delete_red.png" CommandName="Delete" CommandArgument='<%# Eval("AuditMgmt_DeptLocFloor_ID") %>'  ToolTip="Click to delete this department location floor"  />
                                </ItemTemplate>
                                </asp:TemplateField>
               </Columns>              
               </asp:GridView>    
               </div>          
               </center>
                </div>  
                </ContentTemplate>
                </asp:UpdatePanel>
0
TrialUserAuthor Commented:
Otherwise, can I access the variable dtDept , which is the datasource for the gridsview and count the no. of rows in the datatable? Thanks
0
leakim971PluritechnicianCommented:
OK so use ID of the UpdatePanel (instead of gdvDept) :
var number_of_rows = document.getElementById("<%= UpdatePanel2.ClientID %>").getElementsByTagName("tr").length; // not good if you've multiple table (own or generated by .NET)

Open in new window

or a test before :
var number_of_rows = 0; // zero by default
var gdvDept = document.getElementById("<%= gdvDept.ClientID %>");
if( gdvDept != null ) gdvDept.getElementsByTagName("tr").length;

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
COBOLdinosaurCommented:

<script>
window.onload = function() {
  if (document.getElementById("<%= gdvDept.ClientID %>")rows.length == 0) {
            showError($('#divError'), "• Please add dept location floor");
        }
}
</script>

Open in new window



Cd&

0
TrialUserAuthor Commented:
That worked for me . Thanks a ton. I really appreciate your constant and quick responses.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
JavaScript

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.