-->

Remembering active tab on refresh of ASPX page

2019-09-02 12:43发布

问题:

I've got an ASPX page set up that loads and displays dynamic data from a local SQLite database. Since the data is being written to the database from a separate C# application, I've set up my ASPX page to refresh every 30 seconds when the database has flagged itself as actively receiving new data.

On my ASPX page, I've got a TabContainer with several different TabPanels that each represent a different view of the data. Now, when my page is being refreshed, the active tab panel is being reset to the one set in my ASPX page as the ActiveTabIndex.

I was wondering if there is an easy way to persist which tab is being remembered.

Thanks!

Edited to add code sample

MyPage.aspx

<%@ Page Title="" Language="C#" MasterPageFile="~/MasterPageFile.master" AutoEventWireup="true" CodeFile="MyPage.aspx.cs" Inherits="MyPage" %>
<%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="asp" %>

<asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">
</asp:Content>

<asp:Content ID="Content2" ContentPlaceHolderID="content" Runat="Server">
    <asp:ToolkitScriptManager ID="ToolkitScriptManager" runat="server" />
    <asp:TabContainer ID="tabContainer" runat="server" ActiveTabIndex="1" CssClass="myTabStyle" Visible="true">
        <asp:TabPanel ID="tab1" runat="server" HeaderText="Tab 1">
            <ContentTemplate>
                <p>Hello</p>
            </ContentTemplate>
        </asp:TabPanel>
        <asp:TabPanel ID="tab2" runat="server" HeaderText="Tab 2">
            <ContentTemplate>
                <asp:PlaceHolder ID="tab2placeholder" runat="server" />
            </ContentTemplate>
        </asp:TabPanel>
        <asp:TabPanel ID="tab3" runat="server" HeaderText="Tab 3">
            <ContentTemplate>
                <asp:TabContainer ID="tab3content" runat="server" ActiveTabIndex="0" CssClass="myTabeStyle" />
            </ContentTemplate>
        </asp:TabPanel>
        <asp:TabPanel ID="tab4" runat="server" HeaderText="Tab 4">
            <ContentTemplate>
                <p>Blah.</p>
            </ContentTemplate>
        </asp:TabPanel>
    </asp:TabContainer>
</asp:Content>

MyPage.aspx.cs

public partial class MyPage : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        ...
        if (isInProgress)
        {
            Response.AddHeader("Refresh", "30");
        }

        LoadTab1();
        LoadTab2();
        LoadTab3();
        LoadTab4();
        ...
    }
}

回答1:

Try putting UpdatePanels within in the TabPanels. If you post a code sample of your ASPX it would be easier to help.



回答2:

If you are doing full postbacks all the time then save it in your ASP.NET session. e.g.

Session["ActiveTab"] = index;

Then to retrieve it:

int? activeTab = Session["ActiveTab"] as int?;

Otherwise using jQuery ajax to refresh the data, is in my experience a much better way to go than using UpdatePanels. But it looks like you are already committed to MS Ajax so go with Mike's recommendation to use an ajax timer instead of a refresh header.