Capturing events from dynamically added custom ASP.NET controls

I have an ASP.NET web form to which I add User Controls variables. I have two problems:

  • User controls are added to PlaceHolder in the form in the first PageLoad event (I only add them when "(! This.IsPostback)", but then when the form is sent back, the controls do not appear. Is this normal? Because other controls on the form retain their state, I would expect these dynamically added to remain in the form. Should I add them for each postback?

  • I also have a button and an event handler for the button click event, but this event handler is never called when I click the button. Is there anything special I have to do to catch events on dynamically added controls?

+4
source share
6 answers
  • Yes, you need to add them to each postback.
  • Yes ... the control must be in the control hierarchy before asp.net dispatches the event (i.e. it will create dynamic controls as early as possible on the page life cycle).
+3
source
  • To achieve this, add controls on the init page instead of loading the page. (re-add to postback)
  • You need to know the identifier of the added buttons in order to associate them with the event.
0
source

1) You must add controls to Pre-init ( page life cycle )

2) You must attach an event handler to the event of the created button (events can occur much later in the page life cycle than the same events for controls created declaratively)

0
source

I had a similar problem. I had a page on which a collection of custom web controls was compiled. My solution was to add an additional invisible web control, so that when I clicked the button to add another control, I would just use the invisible one. Then, when sending back, the download function will add another invisible control to the collection.

0
source

Yesterday, I realized that you can really make your application work fine by loading the control tree immediately after running loadviewstateevent. if you override the loadviewstate event, call mybase.loadviewstate, and then put your own code to regenerate the controls immediately after it, the values ​​for these controls will be available when the page loads. In one of my applications, I use the viewstate field to store identifier or array information that can be used to recreate these controls.

Protected Overrides Sub LoadViewState(ByVal savedState As Object) MyBase.LoadViewState(savedState) If IsPostBack Then CreateMyControls() End If End Sub 
0
source

I ran into the same problem and struggled after 5-6 hours. I am posting this, maybe someone like me can get help.

1) You must initialize your controls in the Page.PreInit event. (In my case, I had to add my controls to the place holder, so I pre-expanded PreInit to load these controls, but you don't need to do this. It depends on your scenario.)

2) You must bind these exact methods to your controls after they are initialized in your Page.PreInit event.

Here is my sample code:

 protected override void OnPreInit(EventArgs e) { // Loading controls... this.PrepareChildControlsDuringPreInit(); // Getting ddl container from session and creating them... if (GetDDLSession().Count != 0) { foreach (DropDownList ddl in GetDDLSession()) { ddl.SelectedIndexChanged += SelectedIndexChanged; phDropDowns.Controls.Add(ddl); } } base.OnPreInit(e); } public static void PrepareChildControlsDuringPreInit(this Page page) { // Walk up the master page chain and tickle the getter on each one MasterPage master = page.Master; while (master != null) master = master.Master; } 
0
source

All Articles