Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 771
  • Last Modified:

Using AJAX timer to refresh data

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
Relegence
Asked:
Relegence
  • 2
1 Solution
 
sybeCommented:
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
 
RelegenceAuthor Commented:
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
 
sybeCommented:
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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now