Swapping programmatically created user controls?

Discussion in 'ASP .Net' started by Ramesh, Aug 16, 2006.

  1. Ramesh

    Ramesh Guest

    Hello all,

    I have a form in which I'm trying to load a user control depending on
    some user choice, and my control's events are not firing properly.
    Here's a completely stripped down repro of the problem:

    The form itself is empty:
    <body>
    <form id="form1" runat="server" />
    </body>

    and here's the code behind for the form:
    protected override void OnInit( EventArgs e )
    {
    string currentControl = "UC1";
    object objCurrentControl = Session["CurrentControl"];
    if( objCurrentControl != null )
    currentControl = (string)objCurrentControl;

    form1.Controls.Add( LoadControl( currentControl + ".ascx" ) );
    }


    The first user control:
    ===============
    <%@ Control Language="C#" AutoEventWireup="true" CodeFile="UC1.ascx.cs"
    Inherits="UC1" %>
    <asp:Button ID="Button1" runat="server" OnClick="Button1_Click"
    Text="Create UC2" />

    and the code behind:
    protected void Button1_Click( object sender, EventArgs e )
    {
    Control ctl = LoadControl( "UC2.ascx" );
    ctl.ID = "UC2";
    Parent.Controls.Add( ctl );
    HttpContext.Current.Session["CurrentControl"] = "UC2";
    Parent.Controls.Remove( this );
    }

    The second user control:
    ==================
    <%@ Control Language="C#" AutoEventWireup="true" CodeFile="UC2.ascx.cs"
    Inherits="UC2" %>
    <asp:Button ID="Button1" runat="server" OnClick="Button1_Click"
    Text="Create UC1" />

    and the code behind:
    protected void Button1_Click( object sender, EventArgs e )
    {
    Control ctl = LoadControl( "UC1.ascx" );
    ctl.ID = "UC1";
    Parent.Controls.Add( ctl );
    Parent.Controls.Remove( this );
    HttpContext.Current.Session["CurrentControl"] = "UC1";
    }

    and here's the problem:
    When I run this, the page loads fine, with UC1. I click on the button
    in UC1, the form gets posted, and comes back with UC2. Now if I click
    on the button in UC2, the form gets posted, but comes back with UC2
    itself instead of UC1. I click on the button in UC2 again, and now the
    form comes back with UC1! It seems that the control loads fine, but the
    button click event defined inside the user control will not fire the
    first time, but only the second time.

    I'm from the WinForms world and I'm new to the WebForms, so I might be
    missing something obvious. Could anyone kindly point out what I'm doing
    wrong? I've been scratching my head for the past two days without
    making any progress. Thanks a lot in advance for your time.

    - Ramesh.
     
    Ramesh, Aug 16, 2006
    #1
    1. Advertising

  2. Ramesh

    Ramesh Guest

    Hi,

    I solved the problem myself. All I had to do was change the OnInit of
    the form to set the ID of the control as well, like this:

    protected override void OnInit( EventArgs e )
    {
    string currentControl = "UC1";
    object objCurrentControl = Session["CurrentControl"];
    if( objCurrentControl != null )
    currentControl = (string)objCurrentControl;

    Control ctl = LoadControl( currentControl + ".ascx" );
    ctl.ID = currentControl;
    form1.Controls.Add( ctl );
    }

    and it works fine now. I knew I was missing something obvious... Sorry
    if I had wasted your time.

    This approach, with an Atlas UpdatePanel thrown in to enclose the user
    controls, results in a very WinForms like feel. Wow!

    - Ramesh

    Ramesh wrote:
    > Hello all,
    >
    > I have a form in which I'm trying to load a user control depending on
    > some user choice, and my control's events are not firing properly.
    > Here's a completely stripped down repro of the problem:
    >
    > The form itself is empty:
    > <body>
    > <form id="form1" runat="server" />
    > </body>
    >
    > and here's the code behind for the form:
    > protected override void OnInit( EventArgs e )
    > {
    > string currentControl = "UC1";
    > object objCurrentControl = Session["CurrentControl"];
    > if( objCurrentControl != null )
    > currentControl = (string)objCurrentControl;
    >
    > form1.Controls.Add( LoadControl( currentControl + ".ascx" ) );
    > }
    >
    >
    > The first user control:
    > ===============
    > <%@ Control Language="C#" AutoEventWireup="true" CodeFile="UC1.ascx.cs"
    > Inherits="UC1" %>
    > <asp:Button ID="Button1" runat="server" OnClick="Button1_Click"
    > Text="Create UC2" />
    >
    > and the code behind:
    > protected void Button1_Click( object sender, EventArgs e )
    > {
    > Control ctl = LoadControl( "UC2.ascx" );
    > ctl.ID = "UC2";
    > Parent.Controls.Add( ctl );
    > HttpContext.Current.Session["CurrentControl"] = "UC2";
    > Parent.Controls.Remove( this );
    > }
    >
    > The second user control:
    > ==================
    > <%@ Control Language="C#" AutoEventWireup="true" CodeFile="UC2.ascx.cs"
    > Inherits="UC2" %>
    > <asp:Button ID="Button1" runat="server" OnClick="Button1_Click"
    > Text="Create UC1" />
    >
    > and the code behind:
    > protected void Button1_Click( object sender, EventArgs e )
    > {
    > Control ctl = LoadControl( "UC1.ascx" );
    > ctl.ID = "UC1";
    > Parent.Controls.Add( ctl );
    > Parent.Controls.Remove( this );
    > HttpContext.Current.Session["CurrentControl"] = "UC1";
    > }
    >
    > and here's the problem:
    > When I run this, the page loads fine, with UC1. I click on the button
    > in UC1, the form gets posted, and comes back with UC2. Now if I click
    > on the button in UC2, the form gets posted, but comes back with UC2
    > itself instead of UC1. I click on the button in UC2 again, and now the
    > form comes back with UC1! It seems that the control loads fine, but the
    > button click event defined inside the user control will not fire the
    > first time, but only the second time.
    >
    > I'm from the WinForms world and I'm new to the WebForms, so I might be
    > missing something obvious. Could anyone kindly point out what I'm doing
    > wrong? I've been scratching my head for the past two days without
    > making any progress. Thanks a lot in advance for your time.
    >
    > - Ramesh.
     
    Ramesh, Aug 17, 2006
    #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. Rob Meade
    Replies:
    9
    Views:
    585
    Rob Meade
    Aug 10, 2004
  2. Guest
    Replies:
    4
    Views:
    7,173
    Steven Cheng[MSFT]
    Nov 9, 2005
  3. TB
    Replies:
    0
    Views:
    414
  4. Replies:
    0
    Views:
    419
  5. Replies:
    3
    Views:
    189
Loading...

Share This Page