Dynamically loaded user control events not firing on first click

Discussion in 'ASP .Net' started by Earl Teigrob, Feb 9, 2004.

  1. Earl Teigrob

    Earl Teigrob Guest

    PROBLEM:
    When a user control is loaded into a PlaceHolder control more than once, the
    events do not fire on the first click of a control on the dynamically loaded
    user control. In other words, the first time the control is dynamically
    loaded, everything works fine. After that, if the control is loaded again
    from the page button event handler, the user controls events fail to fire on
    the first click

    NOTE: I (believe I) am rebuilding all of the controls each time.

    EXAMPLE:
    I have created a very simple example to illistrate this.
    In this example, when the button on the page is pressed once, it loads the
    user control and the events of the user control work fine. If, however, the
    page button is pressed again, the control is reloaded into the placeholder
    but the user control events are lost on the first click of an event firing
    control (button in this example). After the first click on the user control
    button, the control buttons events start working again.

    I am using this in a much more complex senerio and need to find out what I
    am doing wrong.

    Thank you for your help

    Earl

    USER CONTROL:
    The user control consists of one button and a literal and the botton event
    is:

    private void Button1_Click(object sender, System.EventArgs e)
    {

    Literal1.Text="Hello World";
    }

    ASPX PAGE THAT LOADS CONTROL
    The loading page consists of a placeholder control and a button with the
    following property and event handlers

    private void Page_Load(object sender, System.EventArgs e)
    {
    if (ControlLoaded)
    LoadUserControl();
    }
    private void Button1_Click(object sender, System.EventArgs e)
    {
    LoadUserControl();
    }

    private void LoadUserControl()
    {
    PlaceHolder2.Controls.Clear();

    PlaceHolder2.Controls.Add(Page.LoadControl("~/test/DynamicLoadUserCont.ascx"
    ));
    ControlLoaded=true;
    }

    public bool ControlLoaded
    {
    get
    {
    Object s = ViewState["ControlLoaded"];
    return ((s==null)?false:(bool)s);
    }

    set
    {
    ViewState["ControlLoaded"] = value;
    }
    }
    Earl Teigrob, Feb 9, 2004
    #1
    1. Advertising

  2. Earl Teigrob

    nfedin Guest

    You problem is that you do not give you dynamically loaded control an
    ID. So .Net gives your control it's own name, usually something like
    _ctl2 or _ctl3. What is happening is that the name given to your
    control is changing. If you explicitly define the ID, this won't
    happen.

    i.e. MyControl.ID = "MyControl"

    The best way to understand this is to use Tracing in the web.config
    and look at the control tree.

    Neil

    "Earl Teigrob" <> wrote in message news:<#>...
    > PROBLEM:
    > When a user control is loaded into a PlaceHolder control more than once, the
    > events do not fire on the first click of a control on the dynamically loaded
    > user control. In other words, the first time the control is dynamically
    > loaded, everything works fine. After that, if the control is loaded again
    > from the page button event handler, the user controls events fail to fire on
    > the first click
    >
    > NOTE: I (believe I) am rebuilding all of the controls each time.
    >
    > EXAMPLE:
    > I have created a very simple example to illistrate this.
    > In this example, when the button on the page is pressed once, it loads the
    > user control and the events of the user control work fine. If, however, the
    > page button is pressed again, the control is reloaded into the placeholder
    > but the user control events are lost on the first click of an event firing
    > control (button in this example). After the first click on the user control
    > button, the control buttons events start working again.
    >
    > I am using this in a much more complex senerio and need to find out what I
    > am doing wrong.
    >
    > Thank you for your help
    >
    > Earl
    >
    > USER CONTROL:
    > The user control consists of one button and a literal and the botton event
    > is:
    >
    > private void Button1_Click(object sender, System.EventArgs e)
    > {
    >
    > Literal1.Text="Hello World";
    > }
    >
    > ASPX PAGE THAT LOADS CONTROL
    > The loading page consists of a placeholder control and a button with the
    > following property and event handlers
    >
    > private void Page_Load(object sender, System.EventArgs e)
    > {
    > if (ControlLoaded)
    > LoadUserControl();
    > }
    > private void Button1_Click(object sender, System.EventArgs e)
    > {
    > LoadUserControl();
    > }
    >
    > private void LoadUserControl()
    > {
    > PlaceHolder2.Controls.Clear();
    >
    > PlaceHolder2.Controls.Add(Page.LoadControl("~/test/DynamicLoadUserCont.ascx"
    > ));
    > ControlLoaded=true;
    > }
    >
    > public bool ControlLoaded
    > {
    > get
    > {
    > Object s = ViewState["ControlLoaded"];
    > return ((s==null)?false:(bool)s);
    > }
    >
    > set
    > {
    > ViewState["ControlLoaded"] = value;
    > }
    > }
    nfedin, Feb 10, 2004
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?U2hvdXJpZQ==?=
    Replies:
    1
    Views:
    7,872
    =?Utf-8?B?U2hvdXJpZQ==?=
    Jan 21, 2004
  2. Raed Sawalha
    Replies:
    1
    Views:
    604
    Karl Seguin
    Oct 7, 2004
  3. =?Utf-8?B?U3RldmUgQm9vdGg=?=

    Event fails to fire first time in dynamically loaded user control(

    =?Utf-8?B?U3RldmUgQm9vdGg=?=, Apr 5, 2005, in forum: ASP .Net
    Replies:
    6
    Views:
    579
    =?Utf-8?B?U3RldmUgQm9vdGg=?=
    Apr 6, 2005
  4. =?Utf-8?B?Smlt?=
    Replies:
    2
    Views:
    597
    =?Utf-8?B?Smlt?=
    Aug 20, 2007
  5. SevDer
    Replies:
    2
    Views:
    1,285
    Walter Wang [MSFT]
    Nov 13, 2007
Loading...

Share This Page