Solved

Using AJAX timer to refresh data

Posted on 2009-04-07
3
763 Views
Last Modified: 2012-05-06
Hello,

I am writing a ASP.NET C# application and using a AJAX timer in order to refresh my data every couple of seconds.

I have a list of tabs which its selection is reset after every refresh.
It is like the whole page is rendered again.

I need to keep the selected tab 'selected' after refresh.

How can I achieve that?
I am attaching the relevant code.

Thank you
ASPX
--------
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="test.aspx.cs" Inherits="InternalMonitorTool.test" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 
<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
    <title>Untitled Page</title>
    <link href="css/tabs.css" rel="stylesheet" type="text/css">
    <script language="javascript" type="text/javascript">
        function selectTab(tabid)
        {
            try
            {
                var arrTabs = new Array("tabCounters","tabTopFeed");
                for (var currTab in arrTabs)
                {
                    document.all(arrTabs[currTab]).className = "notSelected";
                }   
 
                document.all("selectedTabHidden").value = tabid;
                document.all(tabid).className = "selected";
            }
            catch(e)
            {
            }
        }
    </script>
 
</head>
<body>
    <form id="form1" runat="server">
    <asp:ScriptManager ID="ScriptManager1" runat="server" />
    <div id="navcontainer">
        <ul id="tablist" runat="server">
            <li><a id="tabCounters" class="selected" href="#" onclick="selectTab('tabCounters')">Counters</a></li>
            <li><a id="tabTopFeed" class="selected" href="#" onclick="selectTab('tabTopFeed')">Top Feed</a></li>
        </ul>
    </div>
    
    <input type="text" runat="server" id="selectedTabHidden" />
    
    <asp:Timer ID="TimerCounters" runat="server" OnTick="Timer_Tick" Interval="10000" />
    </form>
</body>
</html>
 
 
ASPX.CS
------------
protected void Timer_Tick(object sender, EventArgs e)
        {
            string selectedTab = selectedTabHidden.Value;
            ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "showTab", "selectTab('" + selectedTab + "');", true);
        }
 
CSS
------
#navcontainer {
        margin: 10px 0 0 30px;
        padding: 0;
}
 
#navcontainer ul {
        border: 0;
        margin: 0;
        padding: 0;
        list-style-type: none;
        text-align: center;
}
 
#navcontainer ul li {
        display: block;
        float: left;
        text-align: center;
        padding: 0;
        margin: 0;
}
 
#navcontainer ul li a {
        background: #fff;
        width: 128px;
        height: 18px;
        border-top: 1px solid #6693c7;
        border-left: 1px solid #6693c7;
        border-bottom: 1px solid #6693c7;
        border-right: 0px solid #6693c7;
        padding: 0;
        margin: 0 0 10px 0;
        color: #6693c7;
        text-decoration: none;
        display: block;
        text-align: center;
        font: bold 12px/18px verdana, lucida, sans-serif;
}
 
#navcontainer ul li a:hover {
        color: navy;
        background: #d0e4fb;
}
 
#navcontainer a:active {
        background: #d0e4fb;
        color: Navy;
}
 
#navcontainer li#active a {
        background: #d0e4fb;
        border: 1px solid #c60;
        color: Navy;
}
 
#navcontainer li a.current{
        background: #d0e4fb;
        color: Navy;
}
 
#navcontainer li a.selected{
        background: #d0e4fb;
        color: Navy;
}
 
 
#navcontainer li a.notSelected{
        background: #ffffff;
        color: #6693c7;
}

Open in new window

0
Comment
Question by:Relegence
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 28

Expert Comment

by:sybe
ID: 24087080
I guess you AJAX is updating too much of the page. You could use an AJAX script that only updates the values of the textnodes with variable content, and not such a big part of the page.

Alternative you could store the current focus in the part of the page that is not updated by Ajax, and right after the update, restore the focus.

And for a third solution, you could pass the focus as a parameter to the AJAX-script and get the piece of HTML returned with the right focus.
0
 

Author Comment

by:Relegence
ID: 24088689
Thank you very much for your answer.

Can you please give an example for the second solution or point me to a url with such an example?
0
 
LVL 28

Accepted Solution

by:
sybe earned 500 total points
ID: 24090824
Actually you could use the function selectTab(tabid), if you'd store the value of the selected tabid in a public javascript variable on the client.

When the ajax call is finished, you'd just need to call with javascript (on the client) the function selectLastTab().
Surely there is some event (at the end of the ajax call) that could fire that function (but I am afraid that I can not help you very well with that).

Basically your "protected void Timer_Tick(object sender, EventArgs e)" is trying to do the same, but you would not be here if that worked. I can not tell you exactly why it won't work, maybe because the selectedTabHidden isn't posted to the server with the update.

ASP.Net makes AJAX very complicated. Personally I would pick my first solution, because it is cleaner, faster and easier to code (although this last thing of course depends on where your expertise is)



<script language="javascript" type="text/javascript">
var iSelectedTab = 0;
 
function selectTab(tabid) {
	try {
		var arrTabs = new Array("tabCounters","tabTopFeed");
		for (var currTab in arrTabs) {
			document.all(arrTabs[currTab]).className = "notSelected";
		}   
			document.all("selectedTabHidden").value = tabid;
			document.all(tabid).className = "selected";
			iSelectedTab = tabid;
		} catch(e) {
	}
}
 
function selectLastTab() {
	selectTab(iSelectedTab);
}
 
</script>

Open in new window

0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
This article discusses four methods for overlaying images in a container on a web page
In this tutorial viewers will learn how add a full-size background image to a webpage using CSS3. Create a new HTML document with an internal stylesheet.: In CSS, define the html element to have a background image. Use a high resolution image.: In t…
In this tutorial viewers will learn how to customize the background color and font color of highlighted text using the ::selection element in CSS Begin by defining the selected text as an element in CSS by typing "::selection": Style the ::selection…

738 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question