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
Rate this:
Please Sign up or sign in to vote.
Here I'm going to explain GridView EmptyDataTemplate and FindControl method

Introduction

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.

Background

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

HTML

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.

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

Code-Behind

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:

emptyRow.FindControl("txtEmployeeCode")  
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
        (((TextBox)emptyRow.FindControl("txtEmployeeCode")).Text);
        eInfo.EmployeeName = ((TextBox)emptyRow.FindControl("txtEmployeeName")).Text;
        eInfo.DepartmentId = Convert.ToInt32(((DropDownList)
        emptyRow.FindControl("ddlDepartment")).SelectedValue);
        eInfo.DepartmentName = Convert.ToString(((DropDownList)
        emptyRow.FindControl("ddlDepartment")).SelectedItem.Text);
        eInfo.EmployeeGroup = ((DropDownList)emptyRow.FindControl
        ("ddlEmployeeGroup")).SelectedValue;
        eInfo.Email = ((TextBox)emptyRow.FindControl("txtEmail")).Text;
        eInfo.isActive = ((CheckBox)emptyRow.FindControl("chkActive")).Checked;
        new mainSQL().insertEmployeeInfo(eInfo);
        FillEmployeeGrid();
    }
} 

Summary

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.

License

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

Share

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: http://www.shemeerns.com
Facebook: http://facebook.com/shemeernsblog
Twitter : http://twitter.com/shemeerns
Google+ : http://google.com/+Shemeernsblog
Follow on   Twitter   Google+

Comments and Discussions

 
GeneralMy vote of 5 Pinmemberfquezada11-Sep-12 6:20 

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 | Mobile
Web04 | 2.8.140827.1 | Last Updated 20 Jul 2012
Article Copyright 2012 by Shemeer NS
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid