Ok I got your problem,
Make sure that you Have lable control on the page with ID
lblDateTime
( I think this will be. )
But The Actual Problem Here is First Your Java script event fires and you are getting Right values ( you can varify it by putting
debugger;
to the first line of your JS Function ) to set to your label, but then page Fires server Side event and Page is Getting rendered again and Displays by default Value Applied to Lable.
[Addition to Solution]
Ok As per you Told me that you only want to fire client Side Javascript.
Swapnil 2009 wrote:
OnClientClick="DisplayDateTime()"
Instead Write
OnClientClick = "return DisplayDateTime()"
Swapnil 2009 wrote:
function DisplayDateTime()
{
var DateTime = new Date();
var strYear= DateTime.getYear();
var strMonth= DateTime.getMonth() +1;
var strDay = DateTime.getDate();
var strHours = DateTime.getHours();
var strMinutes = DateTime.getMinutes();
var strSeconds = DateTime.getSeconds();
var dateTime=document.getElementById("lblDateTime");
dateTime.innerText = strDay + "/" + strMonth + "/" + strYear + " " + strHours + ":" + strMinutes + ":" + strSeconds;
}
Instead Write ( Just Appended return false to the end of function )
function DisplayDateTime() {
var DateTime = new Date();
var strYear = DateTime.getYear();
var strMonth = DateTime.getMonth() + 1;
var strDay = DateTime.getDate();
var strHours = DateTime.getHours();
var strMinutes = DateTime.getMinutes();
var strSeconds = DateTime.getSeconds();
var dateTime = document.getElementById("lblDateTime");
dateTime.innerText = strDay + "/" + strMonth + "/" + strYear + " " + strHours + ":" + strMinutes + ":" + strSeconds;
return false;
}
Vote and
Mark as a Answer if it helped.