10

Hey all! I'm loading a UserControl through a Web Method, and using the LoadControl functionality as such:

// create page, stringWriter
Page _page = new Page();
StringWriter _writer = new StringWriter();

// get popup control
Controls_Popup_ForumThreadForm _control = _page.LoadControl("~/Controls/Popup_ForumThreadForm.ascx") as Controls_Popup_ForumThreadForm;

Then I do the following:

// add control to page
 _page.Controls.Add(_control);

HttpContext.Current.Server.Execute(_page, _writer, false);

Problem is that the Page_Load event of the control doesn't fire at all. If I add another function, and call it prior to adding the control to the Controls collection of the page, that function will fire, but the Page_Load event will not fire.

Any ideas, fellas? Thanks all!

1
  • You might add the tags c# and Asp.Net to your tags to get more readers. (And I should check post dates before commenting on 2 year old threads. I was just browsing unanswered questions). Apr 4, 2011 at 20:02

3 Answers 3

5

Based on my knowledge it is not possible to execute events when dynamically rendering controls. But I have a trick in rendering user controls that works.

I use a function to render a user control that take physical path of user control, and a list of properties. I define a special property in the user control, that can be like a method and when I set it run my custom code. This is Render Function:

public static string RenderUserControl(string path, List<KeyValuePair<string, object>> properties)
    {
        Page pageHolder = new Page();
        UserControl viewControl = (UserControl)pageHolder.LoadControl(path);
        viewControl.EnableViewState = false;
        Type viewControlType = viewControl.GetType();
        foreach (var pair in properties)
        {
            PropertyInfo property = viewControlType.GetProperty(pair.Key);
            if (property != null)
            {
                property.SetValue(viewControl, pair.Value, null);
            }
        }
        HtmlForm f = new HtmlForm();

        f.Controls.Add(viewControl);

        pageHolder.Controls.Add(f);
        StringWriter output = new StringWriter();
        HttpContext.Current.Server.Execute(pageHolder, output, false);
        return (output.ToString());
    }

In the user control that you need to render define a property for example, RunMyCode set any property that you want and at last add special property that you define.

data.Add(new KeyValuePair<string, object>("RunMyCode", SomeDataOrNothing));

In the render event of the user control, you can then check the value of the RunMyCode property and if it's set, manually call the methods that the events would normally raise.

0
1

check ASP.NET Page Life Cycle Overview

PreInit:Raised after the start stage is complete and before the initialization stage begins.

Use this event for the following:

Check the IsPostBack property to determine whether this is the first time the page is being processed. The IsCallback and IsCrossPagePostBack properties have also been set at this time.

Create or re-create dynamic controls.

Set a master page dynamically.

Set the Theme property dynamically.

Read or set profile property values.
0

Add the control during the PreInit stage of the page life cycle:

var page = new Page();
var writer = new StringWriter();            
page.PreInit += new EventHandler((s, e) =>
{
    var control = page.LoadControl("");
    (Page)s).Controls.Add(control);
});
HttpContext.Current.Server.Execute(page, writer, false);
1
  • where this code block located? it should be in the page_init i think .. it seems that there is a life cycle problem ... May 6, 2013 at 0:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.