Solved

Using AJAX timer to refresh data

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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Introduction A frequently asked question goes something like this:  "I am running a long process in the background and I want to alert my client when the process finishes.  How can I send a message to the browser?"  Unfortunately, the short answer …
When writing CSS, there are a few simple rules that will make your life easier.    1. Using ‘* {box-sizing:border-box;}’. Using this will wrap all your elements in a nice little compact box-model that will give you the width you want, like so... …
In this Micro Tutorial viewers will learn how to create a CSS image sprite (In a later tutorial, viewers will learn how to use CSS and HTML to create a navigation menu using this sprite) Open a new Photoshop document with a width of (Icon width)x(N…
In this tutorial viewers will learn how to style different bullet points for unordered lists in CSS. Begin with a normal unordered list; the default bullet point is a solid circle: In the CSS, create a defined class of unordered list by typing "ul.S…

760 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now