C# 在 UpdatePanel 上为动态添加的控件动态添加触发器

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19807988/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 15:55:29  来源:igfitidea点击:

Adding Triggers dynamically on UpdatePanel for dynamially added controls

c#asp.netasp.net-ajaxupdatepanel

提问by love Computer science

I am Adding array Buttons to a simple panel dynamically which is located in an Update Panel, now I want to Add triggers for UpdatePanel on click event of these buttons. My codes is as below:

我正在将数组按钮动态添加到位于更新面板中的简单面板,现在我想在这些按钮的单击事件上为 UpdatePanel 添加触发器。我的代码如下:

protected void AddButtons()
{
    Button[] btn = new Button[a];
    for (int q = 0; q < a; q++)
    {

        btn[q] = new Button();

        buttonsPanel.Controls.Add(btn[q]);
        btn[q].ID = "QID" + q;
        btn[q].Click += new EventHandler(_Default_Click);
        btn[q].Attributes.Add("OnClick", "Click(this)");

        AsyncPostBackTrigger trigger = new AsyncPostBackTrigger();
        trigger.ControlID = btn[q].ID;
        trigger.EventName = "Click";
        UpdatePanel2.Triggers.Add(trigger);                
    }
}

Now click event is not fired when i click on any of these bottons and buttons are getting removed.

现在,当我点击任何这些 bottons 和按钮被删除时,点击事件不会被触发。

Please note that these buttons are not available on Page_Init() method.

请注意,这些按钮在 Page_Init() 方法中不可用。

回答by Maxim Kornilov

You need to assign UniqueIDinstead of IDto AsyncPostBackTrigger.ControlID property. Try to use the following code:

您需要分配UniqueID而不是IDAsyncPostBackTrigger.ControlID 属性。尝试使用以下代码:

AsyncPostBackTrigger trigger = new AsyncPostBackTrigger();
trigger.ControlID = btn[q].UniqueID;
trigger.EventName = "Click";
UpdatePanel2.Triggers.Add(trigger);

回答by Atters

I came across this post when I was attempting to dynamically add triggers to an update panel which contained a gridview. I have buttons in the gridview and defining the trigger in the page doesn't work as a unique ID for each button is generated when each row is created.

当我尝试将触发器动态添加到包含 gridview 的更新面板时,我遇到了这篇文章。我在 gridview 中有按钮并且在页面中定义触发器不起作用,因为在创建每一行时会为每个按钮生成一个唯一的 ID。

Generating the trigger like such;

像这样生成触发器;

AsyncPostBackTrigger trigger = new AsyncPostBackTrigger();
trigger.ControlID = btn[q].UniqueID;
trigger.EventName = "Click";
UpdatePanel2.Triggers.Add(trigger);

did not work for me. The control could not be found, however when using the RegisterPostbackControl or the RegisterAysncPostbackControl commands it worked.

对我不起作用。无法找到该控件,但是在使用 RegisterPostbackControl 或 RegisterAysncPostbackControl 命令时它起作用了。

The end example is as follows;

最终示例如下;

    protected void BankAccountDocumentGridView_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
           LinkButton linkButton = (LinkButton)e.Row.Cells[3].FindControl("DocumentsDownloadButton");
           ScriptManager.GetCurrent(Page).RegisterPostBackControl(linkButton);
        }
    }

I figured that the original poster or others who come across this post may benefit from my findings.

我认为最初的海报或遇到这篇文章的其他人可能会从我的发现中受益。