.net-如何注册启动脚本?
时间:2020-03-06 14:38:02 来源:igfitidea点击:
我对.net的经验有限。我的应用程序抛出错误this.dateTimeFormat是未定义的,我跟踪到一个已知的Ajax错误。发布的解决方法说:
"将以下内容注册为启动脚本:"
Sys.CultureInfo.prototype._getAbbrMonthIndex = function(value) { if (!this._upperAbbrMonths) { this._upperAbbrMonths = this._toUpperArray(this.dateTimeFormat.AbbreviatedMonthNames); } return Array.indexOf(this._upperAbbrMonths, this._toUpper(value)); };
那么我该怎么做呢?是否将脚本添加到aspx文件的底部?
解决方案
将其放在页面的页眉部分
我们将使用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) }
我在Web应用程序中遇到了同样的问题(this.datetimeformat是未定义的),的确是由于Microsoft Ajax中的一个错误引起的,并且此函数取代了导致MS Ajax中的错误的函数。
但是上面的代码存在一些问题。这是正确的版本。
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); }
将网页的Page_Load事件放在代码隐藏文件中。如果使用母版页,请将其放在子页中,而不是母版页中,因为子页中的代码将在母版页之前执行,并且如果在母版页的代码中,则仍会如果我们在子页面上使用AJAX,则会出现此错误。