2012-12-26 16 views
4

我想調用在.cs類的aspx頁面上實現的js函數。但是,ScriptManager似乎不存在於.cs類中。基本上,.cs文件是我在項目中使用的dll的一部分。我需要調用在aspx頁面上實現的js函數,從我的.cs文件出現在dll中。ScriptManager無法訪問

的js函數表被成功從aspx頁面調用,但是當我嘗試的.cs相同的代碼文件,它說

的ScriptManager無法訪問由於其保護級別。

這裏的代碼我試圖

protected void MyMethod() 
{ 
    ScriptManager.RegisterStartupScript(this, this.GetType(), "key", "jsfunction();", true); 
} 

爲什麼相同的代碼successfuly aspx頁面上,但不運行從的.cs類的任何想法?

+0

如果只發生在您的網站上,請嘗試在web.confg中設置。 –

回答

1

好,克服了上述問題,我簡單地用這一段代碼

嘗試
System.Web.UI.ScriptManager.RegisterStartupScript(this, this.GetType(), "key", "jsfunction();", true); 

注意在ScriptManager中使用完整的命名空間。

1

ScriptManager.RegisterStartupScript接受Page或Control作爲第一個參數。請確保你通過當前的頁面爲CS方法

protected void MyMethod(Page page) 
{ 
    ScriptManager.RegisterStartupScript(page, typeof(UpdatePanel), new Guid().ToString() , "jsfunction();", true); 
} 

,並使用來自aspx.cs頁面中調用:

MyMethod(this.Page); 
+0

這似乎是訣竅,但現在我面臨另一個問題,我認爲這不是這個問題的一部分。謝謝Akhil – Ibad