Click here to Skip to main content
11,630,646 members (77,844 online)
Click here to Skip to main content

Access Controls from ASP.NET GridView EmptyDataTemplate using FindControl Method

, 20 Jul 2012 CPOL 45.1K 5
Rate this:
Please Sign up or sign in to vote.
Here I'm going to explain GridView EmptyDataTemplate and FindControl method


The empty data row is displayed in a GridView control when the data source that is bound to the control does not contain any records. You can define your own custom user interface (UI) for the empty data row by using the EmptyDataTemplate property. Below I'm giving a sample screen where it's displaying "No Records Found" message if the GridView control does not contain any records and at the same time we can capture new employee information also. Please click here to check the full article.


This code is based on my another article in ASP.NET GridView Insert, Update and Delete, please click here to check the same. Here I'm explaining the code only related to EmptyDataTemplate.

Using the Code


Below, I have given the EmptyDataTemplete ASPX code for the Gridview, I have created a <table> and added the "No Records Found" message in a <tr> and in another <tr> I have given all controls that will be used to enter Employee information in-case of no existing records.

    <table class="grid" cellspacing="0" rules="all" 
    border="1" id="gvEG" style="border-collapse: collapse;">
            <th align="left" scope="col">
                Employee Code
            <th align="left" scope="col">
            <th align="left" scope="col">
            <th align="left" scope="col">
            <th align="left" scope="col">
            <th scope="col">
            <th align="left" scope="col">
            <th scope="col">
        <tr class="gridRow">
            <td colspan="8">
                No Records found...
        <tr class="gridFooterRow">
                <asp:TextBox ID="txtEmployeeCode" runat="server" 
                MaxLength="6" Width="50px"></asp:TextBox>
                <asp:TextBox ID="txtEmployeeName" runat="server" 
                <asp:DropDownList ID="ddlDepartment" runat="server" 
                DataTextField="Name" DataValueField="Id">
                <asp:DropDownList ID="ddlEmployeeGroup" runat="server">
                    <asp:ListItem Text="User" Value="User" 
                    <asp:ListItem Text="Admin" Value="Admin"></asp:ListItem>
                    <asp:ListItem Text="Super User" 
                    Value="Super User"></asp:ListItem>
                <asp:TextBox ID="txtEmail" runat="server" Width="100px" />
                <asp:CheckBox ID="chkActive" runat="server" />
            <td colspan="2" align="justify" valign="middle">
                <asp:LinkButton ID="lnkAdd" runat="server" 
                CausesValidation="false" CommandName="emptyInsert"


In RowCommand, I capture the Link Button click from the EmpltyDataTemlate. Here, we check the commandName and if it has the value"emptyInsert", then create the object of EmployeeInfo.

We can get the EmptyDateTemplate reference in code-behind by the below given code (gvEG is my GridView name).

GridViewRow emptyRow = gvEG.Controls[0].Controls[0] as GridViewRow; 

Once we have the GridViewRow, then we can use the below code to find the controls:

protected void gvEG_RowCommand(object sender, GridViewCommandEventArgs e)
    if (e.CommandName.Equals("emptyInsert"))
        EmployeeInfo eInfo = new EmployeeInfo();
        GridViewRow emptyRow = gvEG.Controls[0].Controls[0] as GridViewRow;
        eInfo.EmployeeCode = Convert.ToString
        eInfo.EmployeeName = ((TextBox)emptyRow.FindControl("txtEmployeeName")).Text;
        eInfo.DepartmentId = Convert.ToInt32(((DropDownList)
        eInfo.DepartmentName = Convert.ToString(((DropDownList)
        eInfo.EmployeeGroup = ((DropDownList)emptyRow.FindControl
        eInfo.Email = ((TextBox)emptyRow.FindControl("txtEmail")).Text;
        eInfo.isActive = ((CheckBox)emptyRow.FindControl("chkActive")).Checked;
        new mainSQL().insertEmployeeInfo(eInfo);


I hope this tip helped you to understand how to find controls from EmptyDataTemplate of ASP.NET GridView. Please check the full article from this link at ASP.NET GridView Insert, Update and Delete.

Please give your valuable suggestions and feedback for further improvements. Thanks for reading.


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


About the Author

Shemeer NS
Software Developer (Senior)
India India
Technology Specialist | CodeProject MVP | Visual Studio Gallery Contributor | Author | Geek | Netizen | Husband | ChessPlayer

Most of my articles are listed on top 5 of the respective 'Best articles of the month' and some of my articles are published on ASP.NET WebSite's Article of the Day section.

Check my contributions in Visual Studio Gallery and Code Project

Technical Blog:
Twitter :
Google+ :

You may also be interested in...

Comments and Discussions

GeneralMy vote of 5 Pin
Reza Ahmadi27-Jun-13 6:02
memberReza Ahmadi27-Jun-13 6:02 
GeneralMy vote of 5 Pin
eparthibanmca22-Apr-13 18:46
membereparthibanmca22-Apr-13 18:46 
Questiongetting null values on accessing EmptyDataTemplate controls Pin
Member 875347024-Nov-12 10:08
memberMember 875347024-Nov-12 10:08 
AnswerRe: getting null values on accessing EmptyDataTemplate controls Pin
Shemeer NS25-Nov-12 8:11
memberShemeer NS25-Nov-12 8:11 
GeneralRe: getting null values on accessing EmptyDataTemplate controls Pin
tara_sh50024-Jul-14 18:37
membertara_sh50024-Jul-14 18:37 
Questionrow is not getting delete in gridview Pin
Ankit Saxena8-Nov-12 17:50
memberAnkit Saxena8-Nov-12 17:50 
AnswerRe: row is not getting delete in gridview Pin
Shemeer NS9-Nov-12 4:23
memberShemeer NS9-Nov-12 4:23 
Questionincreasing Gridviews Rows without inserting into Database Pin
anglo007228-Oct-12 11:54
memberanglo007228-Oct-12 11:54 
QuestionFind Control inside the Gridview On Page load Pin
kuldeep158624-Sep-12 0:26
memberkuldeep158624-Sep-12 0:26 
AnswerRe: Find Control inside the Gridview On Page load Pin
Shemeer NS24-Sep-12 1:56
memberShemeer NS24-Sep-12 1:56 
Is it going inside the foreach loop..
I doubt that the gridview controls row count is zero due to non binding of records, make sure you have assigned the datasource to the gridview.
GeneralRe: Find Control inside the Gridview On Page load Pin
kuldeep158624-Sep-12 2:57
memberkuldeep158624-Sep-12 2:57 
GeneralRe: Find Control inside the Gridview On Page load Pin
anglo007228-Oct-12 13:22
memberanglo007228-Oct-12 13:22 
GeneralMy vote of 5 Pin
fquezada11-Sep-12 6:20
memberfquezada11-Sep-12 6:20 
GeneralMy vote of 5 Pin
Member 343557620-Jul-12 19:39
memberMember 343557620-Jul-12 19:39 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.150723.1 | Last Updated 20 Jul 2012
Article Copyright 2012 by Shemeer NS
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid