How To find control inside EmptyTemplate

In this post, I will show. How to add a new row in gridview. How to find control inside EmptyDataTemplate

<%@ Page Language="C#" EnableEventValidation="false" AutoEventWireup="true" CodeBehind="Gridview.aspx.cs"
   Inherits="JQueryTest.Gridview" %>

<%@ Register Src="Grid3S.ascx" TagName="Grid3S" TagPrefix="uc1" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
   <title></title>
</head>
<body>
   <form id="form1" runat="server">
   <div>
       <asp:GridView ID="grdPrice" DataKeyNames="ID" runat="server" AutoGenerateColumns="False"
           Width="200px" ShowHeader="False" OnRowCommand="grdPrice_RowCommand" OnRowDataBound="grdPrice_RowDataBound"
           CellPadding="4" ForeColor="#333333" GridLines="None">
           <RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
           <Columns>
               <asp:TemplateField>
                   <FooterTemplate>
                       <asp:LinkButton ID="LinkButton1" runat="server" CommandName="Insert">Insert</asp:LinkButton>
                   </FooterTemplate>
               </asp:TemplateField>
               <asp:TemplateField HeaderText="3S">
                   <ItemTemplate>
                       <asp:Label ID="lbl3S" runat="server" Text='<%#Eval("Name")%>'></asp:Label>
                   </ItemTemplate>
                   <FooterTemplate>
                       <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
                   </FooterTemplate>
               </asp:TemplateField>
           </Columns>
           <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
           <PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
           <EmptyDataTemplate>
               <asp:TextBox ID="txtEmptyData" runat="server"></asp:TextBox>
           </EmptyDataTemplate>
           <SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
           <HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
           <EditRowStyle BackColor="#999999" />
           <AlternatingRowStyle BackColor="White" ForeColor="#284775" />
       </asp:GridView>
       <asp:Button ID="btnDelete" runat="server" Text="Delete" OnClick="btnDelete_Click" />
       <asp:Button ID="btnAdd" runat="server" Text="Add More" OnClick="btnAdd_Click" />
   </div>
   </form>
</body>
</html>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

namespace JQueryTest
{
    public partial class Gridview : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindGridView();
            }
        }
        private void BindGridView()
        {
            DataTable dt = new DataTable();
            dt = (DataTable)Session["MyDataSource"];
            grdPrice.DataSource = GetDataSource();
            grdPrice.DataBind();

        }
        protected DataTable GetDataSource()
        {
            const string key = "MyDataSource";
            DataTable dt = Session[key] as DataTable;
            if (dt == null)
            {
                dt = new DataTable();
                dt.Columns.Add("ID", typeof(int)).AutoIncrement = true;
                dt.Columns.Add("Name", typeof(string));
                DataColumn[] keys = new DataColumn[2];
                keys[0] = dt.Columns["ID"];
                dt.PrimaryKey = keys;

                dt.Rows.Add("1", "first object");
                dt.Rows.Add("2", "second object");
                Session[key] = dt;

            }
            return dt;
        }
        protected void AddData()
        {
            DataTable dt = (DataTable)Session["MyDataSource"];
            DataRow rw = dt.NewRow();
            TextBox txt = (TextBox)grdPrice.FooterRow.Cells[1].FindControl("TextBox2");
            if (txt.Text != "")
            {


                rw[1] = txt.Text;
                dt.Rows.Add(rw);
                Session["MyDataSource"] = dt;
            }
        }
        protected void grdPrice_RowCommand(object sender, GridViewCommandEventArgs e)
        {

            if (e.CommandName == "Insert")
            {
                AddData();
                grdPrice.ShowFooter = false;
                BindGridView();
                btnAdd.Enabled = true;

            }
        }

        protected void btnAdd_Click(object sender, EventArgs e)
        {
            GridViewRow gvr;
            if (grdPrice.Rows.Count == 0)
            {
                Table tbl = (Table)grdPrice.Controls[0];
                gvr = (GridViewRow)tbl.Controls[0];
                DataTable dt = (DataTable)Session["MyDataSource"];
                DataRow rw = dt.NewRow();
                TableCell cell = gvr.Cells[0];
                TextBox txtEmptyData = (TextBox)cell.FindControl("txtEmptyData");
                rw[1] = txtEmptyData.Text;
                dt.Rows.Add(rw);
                Session["MyDataSource"] = dt;
                grdPrice.ShowFooter = false;
                BindGridView();

            }
            else
            {
                // when there are rows returned you can reference the gridviewrow by grabbing the footerrow
                gvr = (GridViewRow)grdPrice.FooterRow;
                grdPrice.ShowFooter = true;
                BindGridView();
                btnAdd.Enabled = false;
            }



        }
        DataKey key;
        protected void grdPrice_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                e.Row.Attributes["onmouseover"] = "this.style.cursor='hand';this.style.textDecoration='none';";
                e.Row.Attributes["onmouseout"] = "this.style.textDecoration='none';";
                e.Row.Attributes["onclick"] = ClientScript.GetPostBackClientHyperlink(grdPrice, "Select$" + e.Row.RowIndex);
            }

        }

        protected void btnDelete_Click(object sender, EventArgs e)
        {

            try
            {

                GridViewRow gv = grdPrice.SelectedRow;

                key = grdPrice.DataKeys[gv.RowIndex];
                DataTable dt = (DataTable)Session["MyDataSource"];

                DataRow rw = dt.Rows.Find(int.Parse(key.Value.ToString()));
                rw.Delete();
                dt.AcceptChanges();
                BindGridView();
                grdPrice.SelectedIndex = -1;

            }
            catch
            {
                BindGridView();
            }

        }

    }
}

Please do not post any spam link in the comment box😊

Post a Comment (0)
Previous Post Next Post