Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
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 15-Oct-12 5:19am
Comments
Aman.A at 15-Oct-12 13:56pm
   
can you please post your control markup and hosting page markup?
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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.
  Permalink  
Comments
Member 7966831 at 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 at 16-Oct-12 5:20am
   
Did you tried Page_Init? I doing things like this on Init and it's working fine.
Member 7966831 at 16-Oct-12 5:31am
   
Yea I tried that still not working, maybe another code scripts would help?
n.podbielski at 16-Oct-12 7:39am
   
How are you attaching this event?
Member 7966831 at 16-Oct-12 8:28am
   
I just changed the Page_Load event name to Page_Init
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

I managed to fix that by redirect to another page which redirects just back on the selectedIndexChanged event. Thax anyways guys.
  Permalink  

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

  Print Answers RSS
0 OriginalGriff 6,651
1 Sergey Alexandrovich Kryukov 6,400
2 CPallini 5,230
3 George Jonsson 3,574
4 Gihan Liyanage 2,542


Advertise | Privacy | Mobile
Web01 | 2.8.140921.1 | Last Updated 20 Oct 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100