简体   繁体   中英

My Gridview in asp.net C# row command doesn't fires after update button click

I have created gridview with unbound datasource. So the buttons are created dynamically. there is no interference with asp.net. All things are done dynamically. But the rowcommand in gridview doesn't fires after I click the update button. Can you plz help on this.

namespace WebApplication1
{
public partial class _Default : System.Web.UI.Page
{
    DataTable dt;
    LinkButton RC_1,RC_3,RC_4;
    TextBox TX_01, TX_02, TX_03;
    Button RC_2 ;
    int rowindex,flag = 0;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            dt = new DataTable();
            MakeDataTable();
        }
        else
        {
            dt = (DataTable)ViewState["DataTable"];
            BindGrid();
        }
        ViewState["DataTable"] = dt;
   }

    protected void B_01_Click(object sender, EventArgs e)
    {
        AddToDataTable();
        BindGrid();
        ClearFormData();
        GV.Width = 10; 
    }

    protected void B_02_Click(object sender, EventArgs e)
    {
        LB.Text = "Button 2 is clicked";
    }

    protected void GV_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            RC_1 = new LinkButton();
            RC_2 = new Button();
            RC_1.ID = "id_edit" + e.Row.RowIndex;
            RC_1.Text = "Edit";
            RC_2.ID = "id_delete" + e.Row.RowIndex;
            RC_2.Text = "Delete";
            RC_2.CommandName = "CMD_Delete";
            RC_1.CommandName = "CMD_Edit" ;
            RC_1.EnableViewState = true;
            RC_2.EnableViewState = true;
            //RC_1.Click += new EventHandler(RC_1_Click);
            e.Row.Cells[3].Controls.Add(RC_1);
            e.Row.Cells[4].Controls.Add(RC_2);
        }
   }

    private void MakeDataTable()
    {
        dt.Columns.Add("Date", typeof(string));
        dt.Columns.Add("Time", typeof(string));
        dt.Columns.Add("Number", typeof(string));
        dt.Columns.Add("Edit", typeof(string));
        dt.Columns.Add("Name", typeof(string));
    }
    private void AddToDataTable()
    {
        DataRow NRow = dt.NewRow();
        NRow[0] = TXT_01.Text;
        NRow[1] = TXT_02.Text;
        NRow[2] = TXT_03.Text;
        dt.Rows.Add(NRow);
    }
    private void BindGrid()
    {
        GV.DataSource = dt;
        GV.DataBind();
    }
        private void ClearFormData()
    {
        TXT_01.Text = "";
        TXT_02.Text = ""; 
        TXT_03.Text = "";
    }

        protected void GV_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if((e.CommandName) == "CMD_Update")
            {
                GridViewRow gvr_upd = (GridViewRow)(((LinkButton)e.CommandSource).NamingContainer);
                rowindex = Convert.ToInt32(gvr_upd.RowIndex);
                GV.Rows[rowindex].Cells[1].Controls.Remove(TX_02);
                GV.Rows[rowindex].Cells[0].Text = TX_01.Text;
                GV.Rows[rowindex].Cells[1].Text = "Sandesh";
                GV.Rows[rowindex].Cells[2].Text = TX_03.Text;
                LB.Text = GV.Rows[rowindex].Cells[1].Text + " Updated Successfully ";
                LB.Visible = true;
           }
           if ((e.CommandName) == "CMD_Delete")
           {
                GridViewRow gvr_del = (GridViewRow)(((Button)e.CommandSource).NamingContainer);
                rowindex = Convert.ToInt32(gvr_del.RowIndex) ;
                dt.Rows.RemoveAt(rowindex);
                BindGrid();
                LB.Text = (rowindex + 1) + " Deleted Successfully ";
                LB.Visible = true;
           }
           if ((e.CommandName) == "CMD_Edit")
           {
                GridViewRow gvr_edit = (GridViewRow)(((LinkButton)e.CommandSource).NamingContainer);
                rowindex = Convert.ToInt32(gvr_edit.RowIndex);
                RC_3 = new LinkButton();
                RC_4 = new LinkButton();
                TX_01 = new TextBox();
                TX_02 = new TextBox();
                TX_03 = new TextBox();
                RC_3.ID = "id_update" + rowindex;
                RC_3.Text = "Update ";
                RC_4.ID = "id_cancel" + rowindex;
                RC_4.Text = "Cancel";
                RC_3.CommandName = "CMD_Update";
                RC_4.CommandName = "CMD_Cancel";
                RC_3.CausesValidation = false;
                RC_3.EnableViewState = true;
                RC_4.EnableViewState = true;
                RC_3.Click += new EventHandler(RC_3_Click);
                GV.Rows[rowindex].Cells[3].Text = "";
                GV.Rows[rowindex].Cells[3].Controls.Add(RC_3);
                GV.Rows[rowindex].Cells[3].Controls.Add(RC_4);
                TX_01.Text = GV.Rows[rowindex].Cells[0].Text;
                TX_02.Text = GV.Rows[rowindex].Cells[1].Text;
                TX_03.Text = GV.Rows[rowindex].Cells[2].Text;
                GV.Rows[rowindex].Cells[0].Text = "";
                GV.Rows[rowindex].Cells[1].Text = "";
                GV.Rows[rowindex].Cells[2].Text = "";
                GV.Rows[rowindex].Cells[0].Controls.Add(TX_01);
                GV.Rows[rowindex].Cells[1].Controls.Add(TX_02);
                GV.Rows[rowindex].Cells[2].Controls.Add(TX_03);
           }
        }

        protected void RC_3_Click(object sender, EventArgs e)
        {
                LB.Text = " Vishwas Updated Successfully ";
                LB.Visible = true;
        }

        protected void RC_3_Cclick()
        {
                LB.Text = " Updated Successfully ";
                LB.Visible = true;
        }

}
}
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="WebApplication1._Default" EnableSessionState="True" %>

<!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>Testing Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>

        <asp:TextBox ID="TXT_01" runat="server" MaxLength="10" Width="128px"></asp:TextBox>
        <asp:TextBox ID="TXT_02" runat="server"></asp:TextBox>
        <asp:TextBox ID="TXT_03" runat="server"></asp:TextBox>
        <asp:Button ID="B_01" runat="server" onclick="B_01_Click" Text="Button" />
        <asp:Button ID="B_02" runat="server" onclick="B_02_Click" Text="Button" />
        <asp:Label ID="LB" runat="server">&quot;Hi Answer&quot;</asp:Label>
        <br />

    </div>
    <asp:GridView ID="GV" runat="server" PageIndex="1" 
        Width="309px" EmptyDataText="&quot;No Data&quot;" Font-Names="Arial" ForeColor="Black" 
        UseAccessibleHeader="False" PageSize="5"  
        onrowcommand="GV_RowCommand" onrowdatabound="GV_RowDataBound" 
        EnableViewState="False">
        <RowStyle Font-Names="Arial Black" />
        <EditRowStyle ForeColor="Black" Font-Names="Arial Black" Font-Size="Medium" />
        </asp:GridView>
    </form>
</body>
</html>

You can Attach EventHandler event in GridView before databind() in bind method.

 private void BindGrid()
{
    GV.DataSource = dt;
    GV.RowCommand += new GridViewRowEventHandler(GV_RowCommand);
    GV.DataBind();

}

delegates are best option while creating dynamic controls. Please check below code, I use this in my application for dynamic controls :

updateButton.Click += delegate (System.Object o, System.EventArgs e)
        {
           //GV_RowCommand Binding Code here
        };

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM