.net - 如何注册启动脚本?

mrj*_*hms 7 .net debugging ajax startupscript clientscriptmanager

我对.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));
};
Run Code Online (Sandbox Code Playgroud)

那我该怎么做?我是否将脚本添加到我的aspx文件的底部?

Way*_*yne 9

您将使用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)
}
Run Code Online (Sandbox Code Playgroud)