473,508 Members | 2,363 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

adding user controls at runtime

There has been a lot of articles on how to load user controls at runtime in the Init() method.
UserControl myControl = (UserControl)LoadControl(stringControl);
I add the control in the Init() method so that the viewstate to works properly. What if I want to let the user select what control to display? For instance say I have a drop down list of user controls and want to load the control the user has selected? When the user selects a control the form will auto postback. But at the point Init() method is fired the users selection in the on change event handler for the drop down list has not been set.
So here is what is happening (I think):
Init()
{
//this method adds control to form
AddUserControl(SessionManager.Instance.SomeValue);
}
ddl_IndexChanged()
{
//Add to session so that is available tot he Init() method
SessionManager.Instance.SomeValue = int.Parse(e.Node.DataKey.ToString());
}
So when a user changes a drop down list Init() is called and THEN (the problem) the session variable that tells the AddUserControl() method what control to load is set. So if I were to reload the page without changing the drop down the second time the page would display correctly. Basically the control loaded is always one postback "behind" what the user has selected. Is there any solutions to this problem? I have a lot of instances where it would be nice to load a user control on the fly and I might not know at design time what control I want to load. It my be dictated by user input. I am sure there is a best practices way to accomplish this. It seems like a problem that many people would have run into.

Many thanks in advance!

Posted by JetBrains Omea Reader 341.19

Nov 18 '05 #1
2 2016
Can you not put this code in the page_load event? I don't remember having any
problems with my viewstate when doing this. Although I don't provide the
ability to allow users to load controls but I would imagine this would work
fine.

Ian

"Sam Kuehn" wrote:
There has been a lot of articles on how to load user controls at runtime in the Init() method.
UserControl myControl = (UserControl)LoadControl(stringControl);
I add the control in the Init() method so that the viewstate to works properly. What if I want to let the user select what control to display? For instance say I have a drop down list of user controls and want to load the control the user has selected? When the user selects a control the form will auto postback. But at the point Init() method is fired the users selection in the on change event handler for the drop down list has not been set.
So here is what is happening (I think):
Init()
{
//this method adds control to form
AddUserControl(SessionManager.Instance.SomeValue);
}
ddl_IndexChanged()
{
//Add to session so that is available tot he Init() method
SessionManager.Instance.SomeValue = int.Parse(e.Node.DataKey.ToString());
}
So when a user changes a drop down list Init() is called and THEN (the problem) the session variable that tells the AddUserControl() method what control to load is set. So if I were to reload the page without changing the drop down the second time the page would display correctly. Basically the control loaded is always one postback "behind" what the user has selected. Is there any solutions to this problem? I have a lot of instances where it would be nice to load a user control on the fly and I might not know at design time what control I want to load. It my be dictated by user input. I am sure there is a best practices way to accomplish this. It seems like a problem that many people would have run into.

Many thanks in advance!

Posted by JetBrains Omea Reader 341.19

Nov 18 '05 #2
Hello ENIZIN,
I could possibly add it to the page_load event but I will still have the same problem. Page_load is called before ddl_Index changed method. It is like I have to reload the page after the ddl_Index tell the session vaiable (or whatever) what control to load. Bottom line is that both Init and page_load are called before the event handler for the dropdown list, button or what ever. I guess a work around would be a Response.Redirect back to itself. Problem with that is you loose viewstate info.
Can you not put this code in the page_load event? I don't remember
having any problems with my viewstate when doing this. Although I
don't provide the ability to allow users to load controls but I would
imagine this would work fine.

Ian

"Sam Kuehn" wrote:
There has been a lot of articles on how to load user controls at
runtime in the Init() method.

UserControl myControl = (UserControl)LoadControl(stringControl);

I add the control in the Init() method so that the viewstate to works
properly. What if I want to let the user select what control to
display? For instance say I have a drop down list of user controls
and want to load the control the user has selected? When the user
selects a control the form will auto postback. But at the point
Init() method is fired the users selection in the on change event
handler for the drop down list has not been set.

So here is what is happening (I think):

Init()

{

//this method adds control to form

AddUserControl(SessionManager.Instance.SomeValue);

}

ddl_IndexChanged()

{

//Add to session so that is available tot he Init() method

SessionManager.Instance.SomeValue =
int.Parse(e.Node.DataKey.ToString());

}

So when a user changes a drop down list Init() is called and THEN
(the problem) the session variable that tells the AddUserControl()
method what control to load is set. So if I were to reload the page
without changing the drop down the second time the page would display
correctly. Basically the control loaded is always one postback
"behind" what the user has selected. Is there any solutions to this
problem? I have a lot of instances where it would be nice to load a
user control on the fly and I might not know at design time what
control I want to load. It my be dictated by user input. I am sure
there is a best practices way to accomplish this. It seems like a
problem that many people would have run into.

Many thanks in advance!

Posted by JetBrains Omea Reader 341.19


Nov 18 '05 #3

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

Similar topics

3
1621
by: Dunc | last post by:
I'm writing an app that uses a lot of time-date functions, so I'm trying to create a calendar-type user control that adds a day, month, and year box to a number of places on my page. I can...
2
3705
by: avivgur | last post by:
Hello, I am writing a program in Visual C# and I have encountered a problem. In my program I want to dynamically create a multitude of controls (thousands) on a form. The problem is that calling...
1
1302
by: Linda Wienholt | last post by:
Hi I am upgrading an ASP site where the content is being pulled from a database. On the ASP site the DB content included form tags and other html controls. I want to replace these with a user...
0
1080
by: Mantorok | last post by:
Hi I want to add multiple user controls at runtime when the user clicks a certain button, however everytime I add the control a 2nd time it only adds it once. I understand that on each server...
4
1959
by: rushikesh.joshi | last post by:
Hi All, I have created my own WebControl and want to add it in my aspx page at runtime. it's compiling perfectly, but when i m going to execute, it gives me error of "Object reference not set...
3
1479
by: MikeY | last post by:
Hi everyone, I'm trying to figure out, on my windows form, of what is the best way to add various custom controls at various times during run-time. The problem is that they will need to be in...
3
2062
by: Toe Dipper | last post by:
In short we have a lengthy process when a form is loaded that adds activex controls to our windows form. This process in itself works fine however we would like to push this processing to a thread...
0
1441
by: sonic | last post by:
I am trying to dynamically load a validator and must be missing something elementary here. I extended TextBox control to add some functionality to it. One new feature it contains is IsRequired...
6
11058
by: | last post by:
I have made some user controls with custom properties. I can set those properties on instances of my user controls, and I have programmed my user control to do useful visual things in response to...
0
7223
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,...
0
7321
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
7036
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...
0
7489
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...
0
5624
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,...
1
5047
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...
0
4705
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3191
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
1547
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 ...

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.