![](/img/trans.png)
[英]How to call two javascript function one after the other on onclick event
[英]Call a Javascript function AFTER the Server Onclick event
我的按鈕單擊事件具有以下代碼:
protected void Button1_Click(object sender, EventArgs e)
{
string message = "";
//Series of codes to change the message variable
Page.ClientScript.RegisterStartupScript(GetType(), "", "ShowMessage(" + message +");", true);
}
<script type="text/javascript">
function ShowMessage(msg){
alert(msg);
}
</script>
並且不會顯示該消息。 也許我的RegisterStartupScript不正確?
嘗試以下,
您必須為腳本提供一個有效的名稱。 http://msdn.microsoft.com/zh-CN/library/z9h4dk8y.aspx
啟動腳本由其鍵和類型唯一標識。 具有相同鍵和類型的腳本被視為重復腳本。 頁面上只能注冊具有給定類型和密鑰對的一個腳本。 嘗試注冊已注冊的腳本不會創建該腳本的副本。
ShowMessage
應該在頁面中定義
您必須將參數作為字符串傳遞。 ShowMessage('" + message +"');
最終代碼應該像這樣
Page.ClientScript.RegisterStartupScript(GetType(), "myAlertScript", "ShowMessage('" + message +"');", true);
更新
如果頁面中有更新面板,請嘗試使用以下代碼。
ScriptManager.RegisterClientScriptBlock(this,typeof(Page),"myAlertScript","ShowMessage('" + message +"');", true);
在對ShowMessage
的調用中,您缺少message
變量周圍的引號,請嘗試以下操作:
Page.ClientScript.RegisterStartupScript(this.GetType(), "", "ShowMessage(\"" + message + "\");", true);
要測試消息是否傳遞到客戶端,您可以簡單地調用本機javascript函數,該函數將排除所有客戶端問題。
Page.ClientScript.RegisterStartupScript(GetType(), "", "alert('here?');", true);
您還可以在瀏覽器中查看頁面的源,以查看代碼是否正在交付給客戶端。
要在單擊處理程序(及其調用的所有內容)完成后執行代碼,請在處理程序期間設置超時 。
function onClick() {
window.setTimeout(functionToCall, 0);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.