views:

439

answers:

4

I have a user control that needs to load a child control when a button is clicked. The trouble is that it has to request the control from another class.

So in the button click event, I call the function to get me my control, and add it to the page, like this:

UserControl ctrl = ExampleDataProvider.GetControl(some params...);
myDetailPane.Controls.Add(ctrl);

The GetControl method looks like:

public static UserControl GetControl(some params...)
        {
            ExampleDetailPane ctrl = new ExampleDetailPane();
            ctrl.Value = "12";
            ctrl.Comment = string.Empty;
            return ctrl;
        }

This isn't working due to the page's lifecycle - the Page_Load of the child control gets fired and its controls are null.

I kind-of know that my approach is wrong and why, but don't know the best way to go about fixing it! Could anyone help?

A: 

Two possible approaches:

  1. Have the control in the page but not loaded with anything until they click their button. Then you can populate the values in the control. This has the benefit of being within the page life cycle. Note, you can always use the "display: none" style setting for the that your user control is in. Then, as part of the OnClick for the button, you can reveal the div making your control visible.
  2. You could pop up another window, though obviously this has the potential for being blocked by popup blockers.
Nick DeVore
+1  A: 

If you want to access your control in PostBack or you want to bind Event, you have to create them in CreateChildControls() method.

    private UserControl _uc = null;

/// <summary>
    /// Creates all controls.
    /// </summary>
    /// <remarks>All the controls must be created in this method for the event handler</remarks>
    protected override void CreateChildControls()
    {
        _uc = new UserControl ();

        this.Controls.Add(_uc);
        base.CreateChildControls();
    }
Guillaume Roy
+5  A: 

Dynamic controls must be re-created on every postback, this Article is a good link about how to persist dynamic controls and their state.

Richard Friend
+2  A: 

Create your control in Page_Init. Then make it visible on your Button_Click event.


(CTRL+C/CTRL+V from some other question I answered last week):

Everything that has to be maintained between page cycles should be declared in Page_Init, not Page_Load.

All the initialization, like adding event handlers, and adding controls should be added during initialization, as the state is saved between page cycles. Handling with the content of controls and the viewstate, should be done in Load.

Check also http://msdn.microsoft.com/en-us/library/ms178472.aspx.

Init

Raised after all controls have been initialized and any skin settings have been applied. Use this event to read or initialize control properties.

.

Load

The Page calls the OnLoad event method on the Page, then recursively does the same for each child control, which does the same for each of its child controls until the page and all controls are loaded.

Use the OnLoad event method to set properties in controls and establish database connections.

Jan Jongboom