views:

388

answers:

2

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  A: 

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

Mike Cellini
I added some code. I'm not sure how an update panel would help this? Doesn't the TabContainer already use update panels to implement the tab functionality? Or are you suggesting that I should put an UpdatePanel in each tab panel that periodically checks for new data? How would I go about doing this?
sohum
I'm not sure how it's tab functionality is implemented but I don't think it uses update panels because a tab change doesn't necessarily require a trip to the server it is likely just implemented with some javascript functions.That said, I think your page is doing a full refresh instead of a partial postback. Try using an ajax timer instead of the refresh header.You will probably need to wrap your TabContainer in an UpdatePanel in order to force the data to render.
Mike Cellini
Great, this works perfectly. I created one timer and an UpdatePanel within each TabPanel's ContentTemplate. The only remaining issue right now is that upon the postback, the tab focus is switched to the top (not where the user has scrolled). Can this be easily fixed? It's not a huge issue.
sohum
You might try wrapping the entire TabContainer in an UpdatePanel rather than each TabPanel. Other than that I can't think of any way of the top of my head.
Mike Cellini
Wrapping the whole thing in a TabContainer causes the original hardcoded tab to be set as active, same as the Response.Refresh behavior.
sohum
+1  A: 

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.

Daniel Lee