Click here to Skip to main content
Click here to Skip to main content

Access Controls from ASP.NET GridView EmptyDataTemplate using FindControl Method

, 20 Jul 2012 CPOL
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+ :
Follow on   Twitter   Google+

Comments and Discussions

GeneralMy vote of 5 PinmemberReza Ahmadi27-Jun-13 7:02 
GeneralMy vote of 5 Pinmembereparthibanmca22-Apr-13 19:46 
Questiongetting null values on accessing EmptyDataTemplate controls PinmemberMember 875347024-Nov-12 11:08 
AnswerRe: getting null values on accessing EmptyDataTemplate controls PinmemberShemeer NS25-Nov-12 9:11 
check this link also
Insert, Update, Delete in ASP.NET Gridview, DataSource as SQL Server, MS Access (mdb/accdb), XML and Framework as 2.0 / 3.0 / 3.5 / 4.0 (VS 2005/2008/2010)[^]
GeneralRe: getting null values on accessing EmptyDataTemplate controls Pinmembertara_sh50024-Jul-14 19:37 
Questionrow is not getting delete in gridview PinmemberAnkit Saxena8-Nov-12 18:50 
AnswerRe: row is not getting delete in gridview PinmemberShemeer NS9-Nov-12 5:23 
Questionincreasing Gridviews Rows without inserting into Database Pinmemberanglo007228-Oct-12 12:54 
QuestionFind Control inside the Gridview On Page load Pinmemberkuldeep158624-Sep-12 1:26 
AnswerRe: Find Control inside the Gridview On Page load PinmemberShemeer NS24-Sep-12 2:56 
GeneralRe: Find Control inside the Gridview On Page load Pinmemberkuldeep158624-Sep-12 3:57 
GeneralRe: Find Control inside the Gridview On Page load Pinmemberanglo007228-Oct-12 14:22 
GeneralMy vote of 5 Pinmemberfquezada11-Sep-12 7:20 
GeneralMy vote of 5 PinmemberMember 343557620-Jul-12 20: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
Web03 | 2.8.150326.1 | Last Updated 20 Jul 2012
Article Copyright 2012 by Shemeer NS
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid