Loading UserControls dynamically


Hendrik W. Hansen

I'm working on a WebForm, that through some tabs can load and contain
a number of different "plugin" UserControls, all derived from the same
"plugin" super class - a bit like the IBuySpy portal.

An XML file defines which plugins are available and should be listed
in my tab control. When the user clicks on as tab, the page should
load the corresponding UserControl into a panel via Page.LoadControl
and Controls.Add.

This works fine, but when the "plugin" controls raises a postback, for
example when a DataGrid in the control raises the SortCommand event,
the control is lost and of course the event handler for SortCommand
will not be executed.

I have tried to save information in the page viewstate about which
control is currently loaded and then in the Page_Load event handler
load the control again, but somehow the control doesn't obtain it's
viewstate from before the postback. For example one of the controls
contains a DataGrid, that looses all it's data, unless I set the
DataSource and calls DataBind again, which should not be necessary.

I guess this all comes down to two things:
1. What do I have to do to ensure that UserControls load via
Page.LoadControl and Controls.Add are present has obtained it's
viewstate after a postback?
2. When in the page process cycle should the above be done?

Best regards
Hendrik W. Hansen

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

Members online

No members online now.

Forum statistics

Latest member

Latest Threads