簡體   English   中英

在沒有Ajax的asp.net回發之后執行javascript函數

[英]Execute javascript function after asp.net postback without Ajax

我希望在asp.net回發之后執行一個javascript函數,而不使用ajax。

我在偶數方法中嘗試了以下方法但沒有運氣:

Page.ClientScript.RegisterStartupScript(GetType(), "ShowPopup", "showCheckOutPopIn('Livraison',556);");

您應該使用ScriptManager類,因為不推薦使用Page.ClientScript屬性...

ClientScriptManager類是ASP.NET 2.0中的新增類,它取代了用於管理現已棄用的腳本的Page類方法。
參考:MSDN - Page.ClientScript屬性

ScriptManager的優點是它可以與異步回發一起使用,因此如果您使用的是AJAX,它將無法與ClientScriptManager一起使用。

您的代碼如下所示:

ScriptManager.RegisterStartupScript(this, this.GetType(), "ShowPopup", "showCheckOutPopIn('Livraison',556);", true);

另請注意,如果您使用的是AJAX並且有一段javascript代碼,您希望在多個回發中執行,那么您應該在第一個參數中引用您的UpdatePanel,例如:

ScriptManager.RegisterStartupScript(MainUpdatePanel, typeof(string), "ShowPopup", "showCheckOutPopIn('Livraison',556);", true);

我需要使用以下重載添加腳本標記。

Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "script", "alert('Success!');", true);

發現: Re:回發后執行javascript

這個人有同樣的問題,我認為和他的解決方案也為我工作:

http://forums.asp.net/t/1121450.aspx?HOW+TO+run+javascript+on+each+partial+postback+

而解決方案就是使用ScriptManager類函數: ScriptManager.RegisterStartupScript(this, typeof(Sections), "Initialize", "initialize();", true);

我不記得Page.ClientScript內容的確切語法/用法是什么......看起來它應該可以隨意工作。 但是如果push推進,只需要一個簡單的用戶控件,你可以動態啟用/禁用,這將在回發后在腳本塊中寫出一個javascript方法。 頁面加載時,此腳本會執行您希望執行的任何功能。

當然,問題是,這個javascript應該做什么,你不能只在服務器端通過一些屬性或其他設置反映在標記?

也許遲到但你可以像這樣使用

母版頁:

   ScriptManager.RegisterStartupScript(this, typeof(Page),
            "ShowRegister", string.Format(@"$( document ).ready(function() {{ShowErrorMessage('{0}');}});", message), true);true);

網頁表格:

 Page.ClientScript.RegisterClientScriptBlock(GetType(), "MEssage",
                          "$( document ).ready(function() {
ShowMessage('{0}');", true);

暫無
暫無

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

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