Click here to Skip to main content
15,886,578 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
A user control I add dynamically has an image with onClick event.
When the user control is added up the onClick event occours only from the second click and on.
Here is the .cs file of the webform:
Public partial class Site_DynamicPage : System.Web.UI.Page
{
    private static Control uc;
    private static string lastControl = null;

    private void loadDynamically()
    {
        if (lastControl != null)
        {
            PlaceHolder1.Controls.Clear();
            uc = Page.LoadControl("~/Site/Controls/" + lastControl + ".ascx");
            PlaceHolder1.Controls.Add(uc);     
        }
    }
    protected void Page_Load(object sender, EventArgs e)
    {
        loadDynamically();
    }
    protected void ListBox1_SelectedIndexChanged(object sender, EventArgs e)
    {
        lastControl = ListBox1.SelectedValue;
        loadDynamically();
    }
}


Here a control is selected from the ListBox and appears on the PlaceHolder,
again the Click event of the user control doesn't fire on the first cilck.
Any solutions for this? Thax.
Posted
Comments
I.explore.code 15-Oct-12 13:56pm    
can you please post your control markup and hosting page markup?

See this:

http://msdn.microsoft.com/en-us/library/kyt0fzt1%28v=vs.100%29.aspx[^]

As it states:
Quote:
Controls are typically added to the page during the page's initialization stage. For details about page stages, see ASP.NET Page Life Cycle Overview.


You adding this during PageLoad. Events are registered earlier.
So they are working after second time when controls were added to page and events registered.
 
Share this answer
 
Comments
Member 7966831 16-Oct-12 4:59am    
If I got you right I should call the loadDynamically function before the Page_Load event. I tried to call the function on PreLoad InitComplete Init and got the same result. However when I tried PreInit it shouted an error of PlaceHolder1 is null.
n.podbielski 16-Oct-12 5:20am    
Did you tried Page_Init? I doing things like this on Init and it's working fine.
Member 7966831 16-Oct-12 5:31am    
Yea I tried that still not working, maybe another code scripts would help?
n.podbielski 16-Oct-12 7:39am    
How are you attaching this event?
Member 7966831 16-Oct-12 8:28am    
I just changed the Page_Load event name to Page_Init
I managed to fix that by redirect to another page which redirects just back on the selectedIndexChanged event. Thax anyways guys.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900