Storing TabContainer's ActiveTabIndex postback

狂风中的少年 提交于 2019-12-11 17:33:16

问题


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?


回答1:


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.



来源:https://stackoverflow.com/questions/6905899/storing-tabcontainers-activetabindex-postback

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!