Swapping programmatically created user controls?

R

Ramesh

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.
 
R

Ramesh

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
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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top