Wednesday, March 21, 2012

Loading UserControls in Tabs

Hello folks,

In my project I am using the AjaxToolKit to display Tabs on my page which is inherieted from a master page Script manager and UpdatePanel is on master page. The aspx page has TabContainer:

<ajaxToolkit:TabContainerID="tcMain"runat="server"OnClientActiveTabChanged="ActiveTabChanged"OnActiveTabChanged="ActiveTabChangedServer">

Also on the aspx page I have this to wireup the client side Tab change event:

<scripttype="text/javascript"language="javascript">
function ActiveTabChanged(sender, e) {
__doPostBack('<%= tcMain.ClientID %>', sender.get_activeTab().get_headerText());
}
</script>

On the code behind of the aspx page I have a method that does the work when the active Tab is changed:

protectedvoid ActiveTabChangedServer(object sender,EventArgs e){
// Do something
}

My Tab container has two tabs each of which loads a User control. What I am noticing is that whenever I switch tabs the previously loaded control has to load again, a postback is occuring. The Tab Panel in the container has ViewState enabled.

Should I have to load the UserConrol again when I switch to a different Tab?

Is there a more efficient way of doing this?

Should I unload the UserControl for better performance and making page lighter when switching tabs? Is this the correct way of doing things?

Appreciate your help.

You could put the tab panel inside of an updatepanel.
You could also set up an handler to check each time the tab is changed and load the last control from viewstate (I like to set up a property that saves any control that I load dynamically into viewstate so I can always refer to it when I need it)...


Take the update panel out of the masterpage and put on the page with the tab container.

Update panels are not intended for masterpages.

Hope this helps

DK


Hi,

You have to load the UserControl again. This is a FAQ about dynamic control in asp.net. Please refer to the following explanation.

1. Why do I have to recreate dynamic controls every time? /Why dynamic controls are disappeared on PostBack?

Whenever a request comes, a new instance of the page that isbeing requested is created to serve the request even it's a PostBack. Allcontrols on the page are reinitialized, and there state can be restored fromthe ViewState in a later phase.
The dynamic controls have to be recreated again and added tothe control hierarchy. Otherwise, they won't exist in the page.
Please be careful with when to create dynamic controls. Inorder to keep their state, they have to be created before the LoadViewStatephase. Page_Init as well as Page_Load methods are options available.
For more information about this topic, please refer to thisarticle:
Creating Dynamic Data Entry User Interfaces[http://msdn2.microsoft.com/en-us/library/aa479330.aspx ]

Hope this helps.

No comments:

Post a Comment