473,657 Members | 2,415 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Swapping programmaticall y created user controls?

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 objCurrentContr ol = Session["CurrentControl "];
if( objCurrentContr ol != null )
currentControl = (string)objCurr entControl;

form1.Controls. Add( LoadControl( currentControl + ".ascx" ) );
}
The first user control:
===============
<%@ Control Language="C#" AutoEventWireup ="true" CodeFile="UC1.a scx.cs"
Inherits="UC1" %>
<asp:Button ID="Button1" runat="server" OnClick="Button 1_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.Cur rent.Session["CurrentControl "] = "UC2";
Parent.Controls .Remove( this );
}

The second user control:
=============== ===
<%@ Control Language="C#" AutoEventWireup ="true" CodeFile="UC2.a scx.cs"
Inherits="UC2" %>
<asp:Button ID="Button1" runat="server" OnClick="Button 1_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.Cur rent.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.

Aug 16 '06 #1
1 2092
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 objCurrentContr ol = Session["CurrentControl "];
if( objCurrentContr ol != null )
currentControl = (string)objCurr entControl;

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 objCurrentContr ol = Session["CurrentControl "];
if( objCurrentContr ol != null )
currentControl = (string)objCurr entControl;

form1.Controls. Add( LoadControl( currentControl + ".ascx" ) );
}
The first user control:
===============
<%@ Control Language="C#" AutoEventWireup ="true" CodeFile="UC1.a scx.cs"
Inherits="UC1" %>
<asp:Button ID="Button1" runat="server" OnClick="Button 1_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.Cur rent.Session["CurrentControl "] = "UC2";
Parent.Controls .Remove( this );
}

The second user control:
=============== ===
<%@ Control Language="C#" AutoEventWireup ="true" CodeFile="UC2.a scx.cs"
Inherits="UC2" %>
<asp:Button ID="Button1" runat="server" OnClick="Button 1_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.Cur rent.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.
Aug 17 '06 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
2075
by: Christopher Jeris | last post by:
Please help me understand the differences, in semantics, browser support and moral preferredness, between the following three methods of swapping content in and out of a page via JavaScript. I would also appreciate any general criticism you have to offer. I don't know yet how to write the degradation-path code for browsers that don't support the DOM methods I'm using, so there are some commented-out paths below. If the content...
5
4847
by: Jonah Olsson | last post by:
Hello guys, I have an application which is built upon several user controls. That is, I have a default template (default.aspx) that I load a user control into (using placeholders in the template). Now, the template also contains a placeholder for buttons used by each user control. The current solution needs each user control to create its buttons, like;
9
1394
by: Rob Meade | last post by:
Hi all, I have a form which is programmatically created from reading values from a database table. There is a 'form' for each DocumentType - when I say form I mean as in a different form will be displayed depending on the DocumentType. If the querystring contains the DocumenType then the drop down menu is removed and a text box (readonly) is put in its place to show the users what
0
1316
by: mark.norgate | last post by:
Hi I'm having a problem in adding controls to a page programmatically in response to a button click. Composite user controls added programmatically in the CreateChildControls() method work fine; they maintain their state as expected. Cool and groovy. However, since CreateChildControls() is called before the method that responds to the button click, say AddControl_Click(), for some reason, this control does not maintain its
8
1988
by: mark.norgate | last post by:
I've asked this question before, but still haven't solved it, so am asking again. I am programmatically adding a user control to the page in response to a button click. The user control consists of three dropdowns and seven text boxes. When the button is clicked, I add another control to the page in Click event of the button and populate the three dropdowns. The text boxes are to be populated by the user.
3
2636
by: Ken Fine | last post by:
I'm interested in programmatically manipulating groups of ASP.NET controls by type. Can someone suggest code for the following? Loop through, say, all label controls on a page, and assigning a CssClass to them, or programmatically making the Visible/not Visible. If applied to a containing page, such a function would traverse all user controls statically and dynamically placed on the page, correct? Thanks,
8
3730
by: hunanwarrior | last post by:
I added textbox controls to a form when user selects amount to create from a combobox as follows: 'Load up the combobox Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load Dim i As Integer For i = 1 To 10 cbxItemCnt.Items.Add(i) Next
2
15065
by: ChrisCicc | last post by:
Hi All, I got a real doozy here. I have read hundreds upon hundreds of forum posts and found numerous others who have replicated this problem, but have yet to find a solution. Through testing I have been able to find the cause of the problem, and will describe it here first textually and then through a code example. The purpose of what I am trying to do is to create a postback-free web application through the use of ASP.net AJAX UpdatePanels...
4
1493
by: Siv | last post by:
Hi, I have an application that reads information from a database and depending on that information creates controls on a form. Depending on where the user is at in a process there may be one item or many items. I create check boxes, one for each entry I get back from the database. The code therefore does stuff like this: cb = New CheckBox tpMain.Controls.Add(cb)
0
8411
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8739
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8513
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8613
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7351
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6176
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4329
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2740
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1969
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.