简体   繁体   English

单击更新按钮后,asp.net C#行命令中的Gridview不触发

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

I have created gridview with unbound datasource. 我用未绑定的数据源创建了gridview。 So the buttons are created dynamically. 因此,按钮是动态创建的。 there is no interference with asp.net. 没有对asp.net的干扰。 All things are done dynamically. 所有事情都是动态完成的。 But the rowcommand in gridview doesn't fires after I click the update button. 但是,单击更新按钮后,gridview中的rowcommand不会触发。 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. 您可以在bind方法中的databind()之前在GridView附加EventHandler事件。

 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
        };

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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