Tuesday, October 11, 2011

Browser timezone

In Page Load add the following.

Page.ClientScript.RegisterStartupScript(this.GetType(), "onLoad",
"script language='javascript' type='text/javascript'>GetTimeZoneOffset();script");


//hdnLocalEndTime is the Hidden variable, which will hold the timezoneoffset.
//Javascript
function GetTimeZoneOffset()
{
var txtLocalEndTime=document.getElementById('hdnLocalEndTime');
var date = new Date()
var gmtHours = - date.getTimezoneOffset()/60;
txtLocalEndTime.value=gmtHours;
}

//Servermethod to calculate time in Browser timezone

string strEndDateTime="it should be the date and time in string format";
DateTime dt;
dt = Convert.ToDateTime(strEndDateTime);
txtEndTime.Text = Convert.ToString(dt.ToUniversalTime().AddHours(Convert.ToDouble(hdnLocalEndTime.Value)));
//txtEndTime.Text will display the time in local time zone

Disbale button during postback

Disbale the button immediately after the click in asp.net.

1. Place the below code in Page_Load event
2. Button should not have OnclientClick ebvent

Button1.Attributes.Add("OnClick", "javascript:" + Button1.ClientID + ".disabled=true;" + ClientScript.GetPostBackEventReference(Button1, ""));