1

我收到一个关于在一组更新面板上添加带有动态 onclick 事件的动态按钮的查询。

我已经简化了这个场景,因为到目前为止我的代码太长并且被捆绑了......我已经创建了一个带有 3 个更新面板的测试页面。

就实际页面而言,第一个更新面板将用于过滤器,过滤器将依次更新第二个更新面板。第二个更新面板将包含所有结果,具体取决于过滤器..这将是一个按钮表。

在第二个更新面板上单击这些按钮中的任何一个时,根据按钮的 ID,将在最后一个更新面板中生成结果。

我面临的问题是在创建按钮时与按钮单击事件相关联。当我从第一个更新面板的 onclick 创建按钮时,它会将其添加到占位符,但 click 事件根本不会触发。

这是我的测试页面中的一些代码。

测试.aspx

<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>

<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
    <asp:Button ID="Button1" runat="server" Text="Button" onclick="Button1_Click" />
</ContentTemplate>

</asp:UpdatePanel>


<asp:UpdatePanel ID="UpdatePanel2" runat="server" ChildrenAsTriggers="False" 
    UpdateMode="Conditional">
<ContentTemplate>

<asp:PlaceHolder id="ph2" runat="server"></asp:PlaceHolder>
</ContentTemplate>

</asp:UpdatePanel>


<asp:UpdatePanel ID="UpdatePanel3" runat="server" UpdateMode="Conditional">

<ContentTemplate>
<asp:PlaceHolder id="ph3" runat="server"></asp:PlaceHolder>
</ContentTemplate>

</asp:UpdatePanel>

代码隐藏:

    public partial class test : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        Button up2button = new Button();
        up2button.ID = "up2button";
        ph2.Controls.Add(up2button);
        up2button.Click += new EventHandler(up2button_Click); // Not being registered?

        AsyncPostBackTrigger trigger1 = new AsyncPostBackTrigger();
        trigger1.ControlID = "up2button";
        trigger1.EventName = "Click";
        UpdatePanel2.Triggers.Add(trigger1);

        ScriptManager1.RegisterAsyncPostBackControl(up2button);

        UpdatePanel2.Update();

    }

    protected void up2button_Click(object sender, EventArgs e) { //and not being fired
        Button up3button = new Button();
        up3button.ID = "up3button";
        up3button.Click += new EventHandler(up3button_click);
        ph3.Controls.Add(up3button);


        AsyncPostBackTrigger trigger1 = new AsyncPostBackTrigger();
        trigger1.ControlID = "up3button";
        trigger1.EventName = "Click";
        UpdatePanel3.Triggers.Add(trigger1);
        UpdatePanel3.Update();
    }

    protected void up3button_click(object sender, EventArgs e) {


    }
}

感谢您的时间。

4

2 回答 2

0

这是一个快速示例,我在互联网上找到:

我使用占位符来保存动态创建的控件 - 该按钮的单击事件上的按钮和文本框。

和代码:

   protected void Page_Load(object sender, EventArgs e)
    {
        if (IsPostBack)
        {
            if (ViewState["Add"] != null)
            {
                Button add = new Button();
                add.Text = "Add";
                add.Click += new EventHandler(add_Click);
                PlaceHolder1.Controls.Add(add);
            }
            if (ViewState["textboxes"] != null)
            {
                int count = 0;
                count = (int)ViewState["textboxes"];
                for (int i = 0; i < count; i++)
                {
                    textbox  textbox_foradd = new TextBox();
                    textbox_foradd.ID = "textadd" + (i + 1).ToString();
                    PlaceHolder1.Controls.Add(textbox_foradd);
                }
            }
        }

    }

    void add_Click(object sender, EventArgs e)
    {
        int count = 1;
        if (ViewState["textboxes"] != null)
        {
            count += Convert.ToInt32(ViewState["textboxes"]);
        }
        TextBox textbox_foradd = new TextBox();
        textbox_foradd.ID = "textadd" + count.ToString();
        PlaceHolder1.Controls.Add(textbox_foradd);
        ViewState["textboxes"] = count;
    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        Button add = new Button();
        add.Text = "Add";
        PlaceHolder1.Controls.Add(add);
        ViewState["Add"] = 1;
    }
于 2012-06-17T21:46:18.220 回答
0

此外,如果您添加多个相同的控件,您需要为每个控件提供唯一的 ID。

例如:添加 ID 为“ButtonA”的按钮 添加另一个 ID 为“ButtonA”的按钮

将失败,回发后您的按钮将正确触发事件,但该操作不会进行正确的刷新。让它看起来不再令人耳目一新。

Instead generate a unique ID for each control you add dynamically (and save those IDs). In the postback, recreate those controls and reassign the IDs properly. Only then will the UI refresh properly.

于 2014-08-18T18:41:35.010 回答