Solved

Using AJAX timer to refresh data

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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Easy css question moving title underneath an image 4 27
Position Absolute Goes Beyond Parent 11 20
Aspx calendar pop up control 3 27
c#, datatable, aspx 4 17
As a result of several questions about how to use Bootstrap I thought it would be a good idea to write down the development aspect of creating a Bootstrapped website in as little time as possible. Part 1 of this article will only concentrate on g…
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
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…
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

763 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