2008-09-23 62 views
7

我对.net的使用经验有限。我的应用程序抛出一个错误this.dateTimeFormat是未定义的,我跟踪到已知的ajax错误。发布的解决方法说:.net - 您如何注册启动脚本?

“注册下面的启动脚本:”

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文件的底部?

回答

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) 
} 
+0

因此,韦恩我会把你的JavaScript的头?我是否需要在名为“myscript”的函数中包装“string str = ...”? – mrjrdnthms 2008-09-24 00:00:04

0

把它放在页面的页眉部分

2

我在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,则会出现错误。