Link to home
Create AccountLog in
Avatar of rich_nouf
rich_nouf

asked on

How do I get events on dynamically loaded user controls to fire when loading controls in a post back?

I am trying to make a Menu control behave asynchronously so in my sitemap datasource I have set the url property to a __doPostBack, eg

<siteMapNode url="javascript:__doPostBack('ctl00$Menu1', 'Test');" title="Test Page" description="Test Page"  />

Where ctl00$Menu1 is the unique id for the menu control on my master page. I then load a user control into the PlaceHolder on my master page (which is contained in an UpdatePanel) based on the __EVENTARGUMENT parameter for the post back, eg

if (IsPostBack)
{
      if (this.Page.Request["__EVENTTARGET"].ToString() == "ctl00$Menu1")
      {
            // load our content based on our postback event argument
            this.ContentPlaceHolder1.Controls.Clear();
            Control control = LoadControl("~/controls/" + this.Page.Request["__EVENTARGUMENT"].ToString() + ".ascx");
            control.ID = this.Page.Request ["__EVENTARGUMENT"].ToString() + "_1";
            this.ContentPlaceHolder1.Controls.Add(control);
      }
}

I have also added a RegisterForEventValidation line to the render method for each user control I want to be able to load, eg

this.Page.ClientScript.RegisterForEventValidation("ctl00$Menu1", "Test");

This all works fine EXCEPT the events for any controls in my usercontrols won't fire. The problem occurs for any usercontrol, eg


<%@ Control Language="C#" AutoEventWireup="true" ClassName="Test" %>
<script runat="server">
    protected void Button1_Click(object sender, EventArgs e)
    {
        // does not fire!
    }
</script>
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Button" />

I have read in various forums that you need to assign an ID to your control when it is loaded dynamically so I have done that as well but it makes no difference.

The question is 'How do I get the control events to fire?'

Oh, and they all fire fine when I change the url for site map node to a page and load the control dynamically in the page_load but then of course my menu does a full page refresh which is what I am trying to avoid.

Thankyou for your help.
Avatar of crazyman
crazyman
Flag of United Kingdom of Great Britain and Northern Ireland image

I presume your adding that control in page load?
Move it to Init or CreateChildControls()

See : http://www.singingeels.com/Articles/Dynamically_Created_Controls_in_ASPNET.aspx
Avatar of rich_nouf
rich_nouf

ASKER

Thanks but I tried loading usercontrol in Page_Init but it makes no difference, and in fact the control event doesn't seem to fire for either case now.
I have attached a text file with sample code showing what I am trying to achieve, which unfortunately due to the fact I can't attach a zip file needs to be recontructed into a web site!

ucevents.txt
When the btnSubmit is clicked the controls never get added in page_init, they get added when the link is clicked but not when the actual button is clicked, they must be re-added EVERY postback...


Im just playing with the code to do this...
ASKER CERTIFIED SOLUTION
Avatar of crazyman
crazyman
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
Thanks, I am currently reading your previous link on re-creating controls on post back.....
Brilliant, that certainly seems to do it for me! Suddenly it all becomes clear about how to recreate controls on post back......thanks.