Geeks With Blogs
AzamSharp Some day I will know everything. I hope that day never comes.

Few days ago I received an email from a reader asking how to access the value of the control contained inside the EmptyDataTemplate of the GridView control. As, you might already know that EmptyDataTemplate is used when there are no records to display. Sometimes you want to give the user an option to add a new item by providing controls inside the EmptyDataTemplate.

 <asp:GridView ID="gvCategories" AutoGenerateColumns="false" runat="server" onrowcommand="gvCategories_RowCommand"
   <asp:TextBox ID="txtCategoryName" runat="server" />
   <asp:Button ID="Btn_AddNewItem" runat="server" CommandName="AddNewItem" Text="Add" />

The above GridView has a TextBox and a Button control inside the EmptyDataTemplate. Now, when the button is clicked we want to access the value inside the TextBox.

 protected void gvCategories_RowCommand(object sender, GridViewCommandEventArgs e)
            if (e.CommandName.Equals("AddNewItem"))
                GridViewRow row = (GridViewRow)((e.CommandSource as Button).NamingContainer);

                string categoryName = (row.FindControl("txtCategoryName") as TextBox).Text;



The line (GridViewRow)((e.CommandSource as Button).NamingContainer); is responsible for extracting the GridViewRow out of the GridView control. Once, you have the GridViewRow simply use the FindControl method to find the control and access the value you want.

Posted on Monday, March 17, 2008 1:51 PM | Back to top

Comments on this post: Accessing Controls Inside EmptyDataTemple in GridView Control

# re: Accessing Controls Inside EmptyDataTemple in GridView Control
Requesting Gravatar...
Ok, but is there a way to access a control before RowCommand()? I want to control visible property of a button with a certain condition.
Left by Jay on May 25, 2008 3:44 PM

Your comment:
 (will show your gravatar)

Copyright © Mohammad Azam | Powered by: