Dynamic contols with LoadControl and ViewState

M

Mark

I have a "Tabbed" ASP.NET page. When a user clicks on a tab, i want
the corresponsing dynamic control to be loaded. This works great
except that the postbacks on that dynamically loaded control do not
work the first time they are attempted. Every subsequent attemp works
great. Here is what is happening

private void Page_Load(object sender, System.EventArgs e)
{
LoadContentControl();

if (!Page.IsPostBack)
{
BindTabs();
BindContent(); }
}

The LoadContentControl loads the correct control based on a variable
stored int the ViewState. If that variable is not initialized that
function will initialize it. Everything works fine and the correct
control loads. Now i have tabs on the page and by clicking on one of
the tabs, a different control should load.

private void TabControl_SelectedTabChanged(object sender, EventArgs e)
{
ViewState["ContentControl"] = TabControl.Tabs.SelectedTab;
LoadContentControl();
BindContent();
}

That works as well, however because the LoadContentControl is not in
the page load ie it happens after, any post back on the dynamically
loaded control will not work, untill the page is loaded again so that
the LoadContentControl in the page load gets fired. So what seems to
be happening is that the LoadContentControl needs to be in the page
load for the postback events need to be handled.

A fix to this would be when the new value for the
ViewState["ContentControl"] is assigned to it in the Tab changed
event, then the page re-initializes or reloads to itself. Then the new
"ContentControl" would be loaded in the first LoadContentControl and
everything would work.

Or if you have any other ideas it would be apreciated

Thanks

Mark
 

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

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top