.net - How do you register a startup script?

I have limited experience with .net. My application throws this.dateTimeFormat undefined error, which I tracked to a known ajax error. A workaround published said:

"Register the following as a script run:"

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? Add script to bottom of my aspx file?

+7
debugging ajax
source share
3 answers

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) } 
+9
source share

I had the same problem in my web application (this.datetimeformat is undefined), indeed, this is due to an error in Microsoft Ajax, and this function overloads the function that causes the error in MS Ajax.

But there are some problems with the code above. Here is 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); } 

Place the Page_Load event of your webpage in the codebehind file. If you use master pages, put them on your child page, not on the main page, because the code on the child pages will run to the main page, and if it is in the code on the main page, you still get an error if you Use AJAX on child pages.

+2
source share

Put it in the page title

0
source share

All Articles