Thursday, July 10, 2008

Fix Dateformat independent of culture

In a project we had a requirement of using the same date format for all cultures. This can be easily centralised in InitializeCulture() event using DateTimeFormat property of CultureInfo. We did it in a base class which is being inherited by all web pages.

CultureInfo cInf = new CultureInfo(cultureName);
cInf.DateTimeFormat.ShortDatePattern ="dd/MM/yyyy";
cInf.DateTimeFormat.DateSeparator = "/";
System.Threading.Thread.CurrentThread.CurrentCulture = cInf;

Register ClientScript

When javascript needs to be registered dynamically based on certain conditions we use page methods like Page.ClientScript.RegisterClientScriptBlock etc.
In case we have an update panel and we need to register a script on partial postback it should be done through script manager. So the code would be.

ScriptManager.RegisterClientScriptBlock(Control, Type, String, String, Boolean)
ScriptManager.RegisterClientScriptBlock(control, Type, key, script, add script tags or not);

A useful blog