Click here to Skip to main content
15,906,097 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a asp.net page containing a LABEL control showing current time.

The way i used for it is,for each timer TICK event,set label.text as Now Time,

The way is enough for me,but when i scroll down to end of the page,its automatically goto beginning of the page after 60 seconds,so that i cant even view the details in the end of the page.


what i can do to avoid this,this also affecting some text fields in the page.
i am using seperate update panel for Timer and other controls



Please help me
Posted
Updated 2-Mar-11 22:26pm
v3

This is happening due to page post back either use jQuery or use update panel to avoid this.

this[^]will help you


--Pankaj
 
Share this answer
 
Comments
kishore Rajendran 2-Mar-11 23:44pm    
hallo thanks for reply,but am using seperate update panel for timer and other controls
pankajupadhyay29 2-Mar-11 23:45pm    
use jquery follow the link i added
You should control your page
if (!IsPostBack)
               {}

this properties.
May be helpful,
Theingi Win
 
Share this answer
 
Hi

You want to show server time or client time?. Alternatively to timer control.

Here is some example showing both client and server time using javascript and ajax.

Have a div sections in the aspx page as shown.
XML
<div><label >Client Time</label><div id="time2"></div></div>
<div><label>Server Time</label>
<asp:TextBox runat="server" ClientIDMode="Static" ID="time1" EnableViewState="false">
</div>

Have this javascript in the header section. If you use master pages then in the master header section.

C#
<script type="text/javascript">
    function getServerTime() {
    var timebox = document.getElementById('time1');
    var xmlhttp;
    if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari
        xmlhttp = new XMLHttpRequest();
    }
    else {
        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
    }
    xmlhttp.onreadystatechange = function () {
        if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
            timebox.innerHTML = xmlhttp.responseText;
        }
        else {
        }
    }
    xmlhttp.open("GET", "Default2.aspx", true);
    xmlhttp.send();
}
function showTime() {
    var d = new Date();
    var curr_hour = d.getHours();
    var curr_min = d.getMinutes();
    var curr_seconds = d.getSeconds();
    if (document.getElementById('time2') != null) {
        document.getElementById('time2').innerHTML = curr_hour + " : " + curr_min + " : "+curr_seconds;
    }
}
setInterval(showTime, 1000); //runs every 1 sec in a separate thread
setInterval(getServerTime, 1000);//runs every 1 sec in a separate thread
function test() {
    showTime();
    getServerTime();
}
</script>


The setInterval function run in separate thread. So it won't interfere with your page unnecessarily.

Add a new webform say Default2.aspx and in that page load event

C#
protected void Page_Load(object sender, EventArgs e)
{
    Response.Write(DateTime.Now.ToString("H : mm : ss"));
}


The Default2.aspx should not have any markup except the page directive


This will be called by the getServerTime javascript function using ajax every 1 sec.

on the body load event call both functions. <body onload="test();">



Hope this helps
 
Share this answer
 
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900