Skip to main content
 首页 » 编程设计

c#之RowCommand 未触发

2024年02月20日12Renyi-Fan

我正在尝试以编程方式在 ASP.NET 中添加一个按钮,以 C# 作为后端。我能够显示该按钮,但当用户单击该按钮时 RowCommand 没有被触发。

我认为问题在于我在用户单击“提交”按钮后创建按钮。如果我在 page_load 中制作按钮,那么 RowCommand 就会起作用。

网站应该是这样的:

  1. 页面加载:显示基础数据。
  2. 用户点击提交按钮创建一个 GridView,它有一个 ButtonField。 GridView 和 ButtonField 都是在后端动态创建的。
  3. 然后用户点击生成的按钮,它应该触发 RowCommand

代码如下:

Page_load

protected void Page_Load(object sender, EventArgs e) 
    { 
     //Nothing because we only want to create the button after the user 
     //clicks on submit    
    } 

randomGridView

生成 GridView 和按钮

public void randomGridView(Table t, int x) 
    { 
 
        GridView gv1 = new GridView(); 
        GridView gv2 = new GridView(); 
        GridView gv3 = new GridView(); 
        GridView gv4 = new GridView(); 
 
        TableRow r = new TableRow(); 
        for (int i = 0; i < x; i++) 
        { 
            TableCell c = new TableCell(); 
            c.BorderStyle = BorderStyle.Solid; 
            if (i == 0) 
            { 
                c.Controls.Add(gv1); 
            } 
            if (i == 1) 
            { 
                c.Controls.Add(gv2); 
            } 
            if (i == 2) 
            { 
                c.Controls.Add(gv3); 
            } 
            if (i == 3) 
            { 
                c.Controls.Add(gv4); 
            } 
            r.Cells.Add(c); 
        } 
        t.Rows.Add(r); 
 
        //Where the xml gets bonded to the data grid 
        XmlDataSource xds = new XmlDataSource(); 
 
        xds.Data = xml; 
        xds.DataBind(); 
        xds.EnableCaching = false; 
 
        gv1.DataSource = xds; 
 
        ButtonField temp = new ButtonField(); 
        temp.ButtonType = ButtonType.Image; 
        temp.ImageUrl = "~/checkdailyinventory.bmp"; 
        temp.CommandName = "buttonClicked"; 
        temp.HeaderText = " "; 
        gv1.Columns.Add(temp); 
 
        gv1.RowCommand += new GridViewCommandEventHandler(CustomersGridView_RowCommand); 
        gv1.RowDataBound += new GridViewRowEventHandler(inventoryGridView_RowDataBound); 
        gv1.DataBind(); 
 
        xds.Data = xml; 
        xds.DataBind(); 
        xds.EnableCaching = false; 
 
        gv2.DataSource = xds; 
        gv2.DataBind(); 
 
 
        xds.Data = xml; 
        xds.DataBind(); 
        xds.EnableCaching = false; 
 
        gv3.DataSource = xds; 
        gv3.DataBind(); 
 
        xds.Data = xml; 
        xds.DataBind(); 
        xds.EnableCaching = false; 
 
        gv4.DataSource = xds; 
        gv4.DataBind(); 
    } 

inventoryGridView_RowDataBound

我尝试在第一个单元格中手动添加一个按钮

public void inventoryGridView_RowDataBound(object sender, GridViewRowEventArgs e) 
    { 
        Button temp = new Button(); 
        temp.CommandName = "buttonClicked"; 
        temp.Text = "temp button!"; 
        e.Row.Cells[0].Controls.Add(temp); 
    } 

CustomersGridView_RowCommand

这就是需要被解雇的东西

public void CustomersGridView_RowCommand(Object sender, GridViewCommandEventArgs e) 
    { 
        if (e.CommandName == "buttonClicked") 
        { 
            int index = Convert.ToInt32(e.CommandArgument); 
            Label1.Text = index.ToString(); 
 
        } 
    } 

Button1_Click

实际上是什么创建了 GridView 和按钮

public void Button1_Click(object sender, EventArgs e) 
    { 
        randomGridView(Table1, 1); 
        randomGridView(Table2, 4); 
    } 

那么如何让按钮触发 CustomersGridView_RowCommand?或者无法链接动态生成的按钮?

编辑:这是 ASPX 页面:

        <table> 
            <tr> 
                <td> 
                    <div id="div1" style="width: 257px; height: 500px; overflow-x: scroll; overflow-y: hidden;"> 
                        <asp:Table ID="Table1" runat="server"> 
                        </asp:Table> 
                    </div> 
                </td> 
                <td> 
                    <div id="div2" style="width: 500px; height: 500px; overflow: scroll;"> 
                        <asp:Table ID="Table2" runat="server"> 
                        </asp:Table> 
                    </div> 
                </td> 
            </tr> 
        </table> 

请您参考如下方法:

这并非不可能。您需要了解,当从 View 状态重新加载页面时,控件树(您用 randomGridView 方法对其进行了补充)会自动重建,但是那些动态添加的控件的事件处理程序不会自动重建。

特别是附加事件处理程序的这一行:

gv1.RowCommand += new GridViewCommandEventHandler(CustomersGridView_RowCommand);  

当您从 View 状态重新加载页面时不会重新执行,当您单击网格中的其中一个按钮并且浏览器将页面发送回服务器时它必须执行。

您的解决方案是在 Load 事件阶段检查页面的回发条件。如果 Page.IsPostBack 为真,则扫描控件树以查找这些动态生成的网格,并为 RowCommand 事件设置事件处理程序(如上面的代码)。