Dynamically adding triggers on UpdatePanel for dynamically added controls

I am adding array buttons to a simple dynamic panel that is in the update panel, now I want to add triggers for UpdatePanel to the click event of these buttons. My codes are as follows:

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 the click event does not fire when I click on any of these buttons and the buttons are removed.

Please note that these buttons are not available when using the Page_Init () method.

+10
source share
3 answers

You need to assign a UniqueID instead of an ID for the AsyncPostBackTrigger.ControlID property. Try using the following code:

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

I came across this post when I was trying to dynamically add triggers to an update panel containing a gridview. I have buttons in a gridview and the trigger definition on the page does not work, since a unique identifier for each button is generated when each row is created.

The generation of such a trigger;

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

didn't work for me. The control cannot be found, however, when using the RegisterPostbackControl or RegisterAysncPostbackControl commands, it worked.

The final 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 realized that the original posters or others that come across this post can benefit from my findings.

+2
source

After searching the Internet for more than I would admit, I found a solution to my answer. I added this line after creating and adding triggers to the update panel.

 ScriptManager.GetCurrent(Page).RegisterAsyncPostBackControl(button); 
0
source

Source: https://habr.com/ru/post/957483/


All Articles