470,631 Members | 1,648 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,631 developers. It's quick & easy.

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 1887
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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by avivgur | last post: by
1 post views Thread by Linda Wienholt | last post: by
reply views Thread by Mantorok | last post: by
4 posts views Thread by rushikesh.joshi | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.