In this video Mohammad Azam will demonstrate how to access controls inside the GridView EmptyDataTemplate.


In this video Mohammad Azam will demonstrate how to access controls inside the GridView EmptyDataTemplate.



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


 public partial class WebForm1 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                BindData();
            }
        }

        private void BindData()
        {
            string connectionString = "Server=localhost;Database=Northwind;Trusted_Connection=true";
            SqlConnection myConnection = new SqlConnection(connectionString);
            SqlDataAdapter ad = new SqlDataAdapter("SELECT * FROM categories where id = 100", myConnection);
            DataSet ds = new DataSet();
            ad.Fill(ds);

            gvCategories.DataSource = ds;
            gvCategories.DataBind();
        }

        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;
                Response.Write(categoryName);

               

            }
        }    
    }

[Play the video]