Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

LoadControl and Page_Load event not firing

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!

like image 832
StephenPAdams Avatar asked Oct 01 '09 16:10

StephenPAdams


2 Answers

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.

like image 87
Mahdi Avatar answered Nov 09 '22 08:11

Mahdi


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.
like image 30
changeme Avatar answered Nov 09 '22 07:11

changeme