2012-12-06 15 views
0

我正在開發ASP.NET頁面。目前頁面上沒有AJAX。在不同按鈕點擊事件的代碼背後,我正在註冊下面的腳本。腳本的目的是將窗口滾動到特定的錨標籤。該方法如下:使用ScriptManager註冊Javascript運行不穩定

public void RegisterAnchor(string anchorTag) 
{ 
    Page.ClientScript.RegisterStartupScript(Page.GetType(), "navigate", 
      "window.onload = function() {window.location.hash='#" + anchorTag + "';}", true); 
} 

我有兩個不同的錨標籤是有效的,並以同樣的方式定義。但是,一個在工作,一個不在。 我可以檢查以確定此行爲的原因是什麼?

錨標籤在ASCX:

<a name="EditDlg"></a>  

回答

0

你可以嘗試註冊使用頁面的情況下客戶端腳本。其他選項是每個部分回發生成唯一的腳本ID。

選項1(註冊頁面實例的客戶端腳本):

ScriptManager.RegisterStartupScript(this.Page, Caller.GetType() , "navigate", "window.onload = function() {window.location.hash='#" + anchorTag + "';}", true); 

選項2(產生獨特的腳本ID每個局部回傳):

ScriptManager.RegisterStartupScript(Caller, Caller.GetType(), Guid.NewGuid(), "window.onload = function() {window.location.hash='#" + anchorTag + "';}", true); 
+0

我修改代碼中的問題,以更清楚。我試過使用獨特的腳本ID,但這並沒有幫助。這不是AJAX相關的,頁面上沒有AJAX,所以我使用Page.ClientScript,或者至少嘗試。謝謝 –