Search code examples
c#asp.netajaxcontroltoolkit

Storing TabContainer's ActiveTabIndex postback


I use the AJAX Control Toolkit's 'TabContainer' control a lot in my application, and a requirement of the application is to store the active tab in a Session. To do this currently, I have the tabs in an UpdatePanel, and perform a postback to store the active tab index in a session when the tabs are changed. This is a slow operation as the page is fairly intensive so this is not ideal. Are there any alternatives to this? Perhaps using a web service to store the session without a post back?


Solution

  • The simplest way is using of ScriptManager's PageMethod.

    1 Enable pagemethods

    <asp:ScriptManager runat="server" EnablePageMethods="true"></asp:ScriptManager>
    

    2 Create pagemethod within aspx-page that'll get actual tab info

      [WebMethod]
      public static void SaveCurrentTab(string controlId, int currentTabIndex)
      {
        // save data to session
      }
    

    3 Create js-function handler for sending actual data to server

    <script type="text/javascript">
    
        function clientActiveTabChanged(sender, args) {
    
            PageMethods.SaveCurrentTab(sender.get_id(), sender.get_activeTabIndex());
        }
    
    </script>
    

    4 Attach js-handler to TabContainer

    <ajaxToolkit:TabContainer ID="TabContainer1" runat="server" OnClientActiveTabChanged="clientActiveTabChanged">
        <ajaxToolkit:TabPanel ID="TabPanel1" runat="server" HeaderText="Test1">
            <ContentTemplate>Test1</ContentTemplate>
        </ajaxToolkit:TabPanel>
        <ajaxToolkit:TabPanel ID="TabPanel2" runat="server" HeaderText="Test2">
            <ContentTemplate>Test2</ContentTemplate>
        </ajaxToolkit:TabPanel>
        <ajaxToolkit:TabPanel ID="TabPanel3" runat="server" HeaderText="Test3">
            <ContentTemplate>Test3</ContentTemplate>
        </ajaxToolkit:TabPanel>
    </ajaxToolkit:TabContainer>
    

    PS There is alternative decision - saving data on client side in cookie.