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.
<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.
<script type="text/javascript">
function getServerTime() {
var timebox = document.getElementById('time1');
var xmlhttp;
if (window.XMLHttpRequest) {
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);
setInterval(getServerTime, 1000);
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
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