Solved

Using AJAX timer to refresh data

Posted on 2009-04-07
3
764 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

Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

Question has a verified solution.

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

This article discusses four methods for overlaying images in a container on a web page
This article discusses how to implement server side field validation and display customized error messages to the client.
In this Micro Tutorial viewers will learn how to create navigation buttons that change on rollover, using CSS (Continuation of the CSS Image Sprite tutorial) Create a parent ID for all the list items       - Specify position: absolute and display: block…
In this tutorial viewers will learn how to style a decorative dropcap for the first letter in a paragraph using CSS. In CSS, create a new paragraph class by typing "p.fancy": Then, to style only the first letter of the first sentence, include the ps…
Suggested Courses

631 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