簡體   English   中英

ScriptManager.RegisterStartupScript代碼不起作用 - 為什么?

[英]ScriptManager.RegisterStartupScript code not working - why?

我過去曾使用這樣的代碼在我的asp.net網頁上成功彈出警報消息。 現在它不起作用。 我無法弄清楚為什么。

ScriptManager.RegisterStartupScript(this, typeof(Page), UniqueID, 
     "alert('This pops up')", true);

有任何想法嗎?

脫離我的頭頂:

  • 使用GetType()而不是typeof(Page)將腳本綁定到實際的頁面類而不是基類,
  • 傳遞一個鍵常量而不是Page.UniqueID ,這不是那么有意義,因為它應該由命名控件使用,
  • 用分號結束你的Javascript語句,
  • PreRender階段注冊腳本:

protected void Page_PreRender(object sender, EventArgs e)
{
    ScriptManager.RegisterStartupScript(this, GetType(), "YourUniqueScriptKey", 
        "alert('This pops up');", true);
}

試試這個代碼......

ScriptManager.RegisterClientScriptBlock(UpdatePanel1, this.GetType(), "script", "alert('Hi');", true);

UpdatePanel1是您網頁上Updatepanelid

如果導致腳本的控件位於updatepanel內,則必須將updatepanel id放在第一個參數中,否則使用關鍵字“this”代替更新面板這里是代碼

ScriptManager.RegisterStartupScript(UpdatePanel3, this.GetType(), UpdatePanel3.UniqueID, "showError();", true);

我遇到了類似的問題。 然而,這個問題是由於我設計頁面以引入請求的方式引起的。我將所有的.js文件放在最后應用於頁面的內容中,因此它們位於我的文檔的末尾。 .js文件包含我的所有功能。 腳本管理器似乎能夠調用此函數它需要js文件已經存在與加載時調用的函數。 希望這有助於其他任何人。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM