【问题标题】:My Gridview in asp.net C# row command doesn't fires after update button click单击更新按钮后,asp.net C# 行命令中的我的 Gridview 不会触发
【发布时间】:2016-07-26 12:23:48
【问题描述】:

我用未绑定的数据源创建了gridview。所以按钮是动态创建的。对asp.net没有干扰。所有事情都是动态完成的。 但是在我单击更新按钮后,gridview 中的行命令不会触发。 你能帮忙吗?

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>

【问题讨论】:

  • 不是编辑按钮而是更新按钮。
  • 您的事件处理程序未附加在 gridview 中。
  • 检查我更新的答案。
  • 我们可以查看您的 .aspx 页面吗?

标签: c# asp.net gridview


【解决方案1】:

您可以在绑定方法中将GridView 中的EventHandler 事件附加到databind() 之前。

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

}

【讨论】:

  • 它给出了错误,因为没有重载方法委托。谢谢帮助
  • 它不适合我。我已经在 asp.net 中为 gv 添加了 rowcommand 事件。
【解决方案2】:

委托是创建动态控件时的最佳选择。请检查下面的代码,我在我的应用程序中使用它来进行动态控制:

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

【讨论】:

    猜你喜欢
    • 2011-09-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-04-10
    • 1970-01-01
    • 2014-01-17
    相关资源
    最近更新 更多