Click here to Skip to main content
15,896,606 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a big problem with a dynamically-created ImageButton...

My scenario is as follows:
  1. I have a "master page" with a ScriptManager control and a usercontrol containing a button.
  2. When I click the button, it opens a ModalPopUpExtender (also contained in that same usercontrol) that contains an UpdatePanel child
  3. Inside that UpdatePanel, there is a table that contains dynamic labels and ImageButtons!
  4. The UpdatePanel is set to Conditional, so when I click an ImageButton, it doesn't lose any table rows...
  5. The problem is that these dynamic controls are NOT REGISTERED to scriptmanager or "AsyncPostBackTrigger" so they don't trigger any postback!


I tried to register them after the form initialization, but I failed:
  1. My ScriptManager is in the parent page and I don't know how to reach it to call RegisterAsyncPostBackControl()... How can I reach my parent control from my ascx?
  2. If I understand correctly, I don't need to register Trigger to my Update Panel after the form init... Is this true? (I read that I do it by calling RegisterAsyncPostBackControl, is this true?)


I wrote this code after the declaration of my dynamic ImageButton:

AsyncPostBackTrigger ImgAddTrigger = new AsyncPostBackTrigger();
ImgAddTrigger.ControlID = ImgAdd.ID + cont.ToString();
ImgAddTrigger.EventName = "Click";
UpdatePanelTabellaPartner.Triggers.Add(ImgAddTrigger);
UpdatePanelTabellaPartner.Update();


...But, it didn't work. So I think that I must call RegisterAsyncPostBackControl.

Thanks in advance for the answers!
If anybody can solve this problem, I think that is the best! I'm going crazy trying to solve this problem!
Posted
Updated 29-Mar-10 10:58am
v3
Comments
bajinder.mann 14-Jun-14 8:41am    
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!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 id="Head1" runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<asp:ScriptManager ID="ScriptManager1" runat="server"
>


<div>


<table width="100%" height="100%">
<tr>
<td>
<asp:Panel ID="Panel1" runat="server"
GroupingText="Buttons Created Dynamically on Page Load">
<table class="style1">
<tr>
<td>
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<contenttemplate>
Problem Statement: These 3 button is being generated Dynamically on Page Load  <br />Result:

<asp:Label ID="lblClick" runat="server">
 <asp:Panel ID="DynamicPanel" runat="server">



</td>
</tr>
</table>

</td>
</tr>
<tr>
<td>
<asp:Panel ID="Panel2" runat="server"
GroupingText="Button Created on Button Click">
<table class="style1">
<tr>
<td>
<asp:UpdatePanel ID="UpdatePanel2" runat="server" UpdateMode="Conditional">
<contenttemplate>
1.Click on the below button<br /> 2.Dynamically created Button click event is
not firing, How do you make the button fire on Click. What solution would you
suggest?<br />
<asp:Button ID="btnClick" runat="server" onclick="btnClick_Click" UseSubmitBehavior="false"
Text="Click to Load Controls" />
   Result:<asp:Label ID="lblResult" runat="server">
<br />
<asp:Panel ID="Panel3" runat="server">



</td>
</tr>
</table>

</td>
</tr>
<tr>
<td>
 </td>
</tr>
<tr>
<td>
 </td>
</tr>
</table>

</div>
</form>
</body>
</html>

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object send
bajinder.mann 14-Jun-14 8:42am    
Please help me .....
dynamic genrated event not working there.

Okay. Here are few things that should help you:

enricosoft wrote:
AsyncPostBackTrigger ImgAddTrigger = new AsyncPostBackTrigger();

Programmatically adding AsyncPostBackTrigger controls is not supported. To programmatically register a postback control, use the RegisterAsyncPostBackControl method of the ScriptManager control. Then call the Update method of the UpdatePanel control when the control posts back. For details:Refer[^].

enricosoft wrote:
My ScriptManager is in the parent page and I don't know how to reach it

C#
ScriptManager currPageScriptManager = ScriptManager.GetCurrent(Page) as ScriptManager;
if (currPageScriptManager != null)
{
 // write your code
 // currPageScriptManager.RegisterAsyncPostBackControl(myControlName);
 // Now call the UpdatePanel.Update() when myControlName posts back.
}

For details: Refer[^]

:thumbsup:
 
Share this answer
 
Hi!
Thanks for the answer... In this case i can reach the ScriptManager situated in my Parent Page and register the AsyncPostBackControl and at the end i Update my UpdatePanel BUT when i click my Dynamic ImageButton,don't work! (using debug mode,the dynamic event associated to my imageButton not run)... :(

Any ideas?
Thanks a lot!
 
Share this answer
 
Hi!,
I tried to apply the change I suggested, but does not work the same (the event not associated dynamic part) ... TI for a while I explain the situation from my master page I click a button that runs a query and according to that I create my dynamic controls in a asptable contained in an UpdatePanel, and then I see my ModalPopUpExtender that shows me my asptable ... I've tried everything and now I am quite desperate)

The tests I've done are:
A) UpdatePanel.Updatemode = always ---> seem to make postback (though actually debugging using visual studio, he does not enter into its associated function), but all dynamic controls disappear and can not be used!


B) UpdatePanel.UpdateMode = conditional ---> Here dynamic controls are maintained. Also recorded "RegisterAsyncPostBackControl" clickable for each dynamic control in ScriptManager in my parent page, it does not postback! (This only because the "AsyncPostBackTrigger" after the form init can not be recorded). The only way to make them do PostBack total record "RegisterPostBackControl" but even so I closed my ModalPopUpExtender. Even as the ListBox control that has AutoPostBack = true does not it! I then tried to create my dynamic control inside another UpdatePanel with UpdateMode = always and I realized that does not work the same! So not only is a problem of 'UpdateMode to me!

1) the problem can be caused by my ModalPopupExtender are created where the different update panel and dynamic controls?

2) Do not think of making a mistake to create my dynamic controls, associate and record an event on the ScriptManager

3) we are sure that there is a way to run the dynamic controls are created after the Form_init? I knew "RegisterAsyncPostBackControl" the ScriptManager but not others ...can "AsyncPostBackTrigger" works well if declared after form_init and form_load?
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900