views:

1942

answers:

3

I have limited experience with .net. My app throws an error this.dateTimeFormat is undefined which I tracked down to a known ajax bug. The workaround posted said to:

"Register the following as a startup script:"

Sys.CultureInfo.prototype._getAbbrMonthIndex = function(value)
{
if (!this._upperAbbrMonths) {
this._upperAbbrMonths = this._toUpperArray(this.dateTimeFormat.AbbreviatedMonthNames);
}
return Array.indexOf(this._upperAbbrMonths, this._toUpper(value));
};

So how do I do this? Do I add the script to the bottom of my aspx file?

A: 

Put it in the header portion of the page

Chris Ballance
+7  A: 

You would use ClientScriptManager.RegisterStartupScript()

string str = @"Sys.CultureInfo.prototype._getAbbrMonthIndex = function(value) { 
    if (!this._upperAbbrMonths) { 
     this._upperAbbrMonths = this._toUpperArray(this.dateTimeFormat.AbbreviatedMonthNames);
    }
    return Array.indexOf(this._upperAbbrMonths, this._toUpper(value));
 };";

if(!ClientScriptManager.IsStartupScriptRegistered("MyScript"){
  ClientScriptManager.RegisterStartupScript(this.GetType(), "MyScript", str, true)
}
Wayne
So Wayne I would put your javascript in the header? Do I need to wrap "string str = ..." in a function called "myscript"?
mrjrdnthms
No, you would just add this code in the code-behind, most likely in the Page Load method. "MyScript" is just a name, so you can check if that particular script has been already been loaded. The code above is written in C#
Wayne
I would by far put this in an external file. There's nothing here that needs to be inline.
steve_c
+1  A: 

Hello,

I had the same problem in my web application (this.datetimeformat is undefined), indeed it is due to a bug in Microsoft Ajax and this function over-rides the error causing function in MS Ajax.

But there are some problems with the code above. Here's the correct version.

string str = @"Sys.CultureInfo.prototype._getAbbrMonthIndex = function(value) { 
    if (!this._upperAbbrMonths) { 
        this._upperAbbrMonths = this._toUpperArray(this.dateTimeFormat.AbbreviatedMonthNames);
    }
    return Array.indexOf(this._upperAbbrMonths, this._toUpper(value));
 };";

ClientScriptManager cs = Page.ClientScript;
if(!cs.IsStartupScriptRegistered("MyScript"))
{
    cs.RegisterStartupScript(this.GetType(), "MyScript", str, true);
}

Put in the Page_Load event of your web page in the codebehind file. If you're using Master Pages, put it in the your child page, and not the master page, because the code in the child pages will execute before the Master page and if this is in the codebehind of Master page, you will still get the error if you're using AJAX on the child pages.

Cyril Gupta