skb
skb

Reputation: 31084

Catching events from dynamically added ASP.NET User Controls

I have an ASP.NET web form which I am adding a variable number User Controls to. I have two problems:

  1. The User Controls are added to a PlaceHolder on the form in the first PageLoad event (I only add them when "(!this.IsPostback)", but then when the form is posted back, the controls are gone. Is this normal? Since other controls on the form keep their state, I would expect these dynamically added ones to stay on the form as well. Do I have to add them for every postback?

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

Upvotes: 4

Views: 1881

Answers (6)

user1409350
user1409350

Reputation:

I ran into the exact same problem and struggled through like 5-6 hours. I'm posting this maybe someone like me could get help.

1) You should initialize your controls at Page.PreInit event. (In my case I had to add my controls to a place holder so I extended PreInit to load those controls before but you don't need to do that. It depends on your scenario.)

2) You should bind those exact methods to your controls after you initialize them 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;
}

Upvotes: 0

Middletone
Middletone

Reputation: 4270

I figured out yesterday that you can actually make your app work like normal by loading the control tree right after the loadviewstateevent is fired. if you override the loadviewstate event, call mybase.loadviewstate and then put your own code to regenerate the controls right after it, the values for those controls will be available on page load. In one of my apps I use a viewstate field to hold the ID or the array info that can be used to recreate those controls.

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

Upvotes: 0

Distantsound
Distantsound

Reputation:

I ran into a similar problem. I had a page that displayed a collection of custom web controls. My solution was to add an additional invisible web control so that when I clicked a button to add another control that I would just use the invisible one. Then on post back my load function would add another invisible control to the collection.

Upvotes: 0

Eppz
Eppz

Reputation: 3206

  1. To achieve this, add your controls at page init instead of page load. (re-add at postback)
  2. You'll need to know the id of the buttons added to bind them to the event.

Upvotes: 0

Eduardo Crimi
Eduardo Crimi

Reputation: 1601

1) You should add the controls on the Pre-init (Page life cycle)

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

Upvotes: 0

Robert C. Barth
Robert C. Barth

Reputation: 23315

  1. Yes, you need to add them in every postback.
  2. Yes... the control needs to be in the control hierarchy before asp.net dispatches the event (i.e. create the dynamic controls as early in the page lifecycle as possible).

Upvotes: 3

Related Questions