2017-03-22 115 views
-2

我想調用JavaScript函數,以防在我的代碼背後發生某些情況。運行按鈕按下按鈕ASP.NET ASP.NET

如果它像下面的代碼,它可以正常工作後回發警報窗口顯示並說。但是,如果我從else塊刪除註釋,那麼else塊中的這兩個腳本都不會發生?

我可以從代碼隱藏中做出多少這些操作,是否有任何限制?

if (condition) { 
    if (condition2) { 
    var message = "It happened !"; 
    Page.ClientScript.RegisterStartupScript(this.GetType(), "yep1", "alert('" + message + "')", true); 
    } 

} else { 
    var msg = "It does not work like that"; 
    Page.ClientScript.RegisterStartupScript(this.GetType(), "nope1", "alert('" + msg + "!')", true); 
    //Page.ClientScript.RegisterStartupScript(this.GetType(), "nope2", "alert('" + msg + "')", true); 
} 

回答

1

這樣它會奏效。隨着功能的名字說,它使你是不是改變它插入2這樣的會做註冊的啓動腳本都^^

if (condition) 
 
{ 
 
    if (condition2) 
 
    { 
 
     var message = "It happened !"; 
 
     Page.ClientScript.RegisterStartupScript(this.GetType(), "yep1", "alert('"+message+"');", true); 
 
    } 
 
} 
 
else 
 
{ 
 
    var msg = "It does not work like that"; 
 
    Page.ClientScript.RegisterStartupScript(this.GetType(), "nope1", "alert('"+msg+"!'); alert('" + msg + "');", true); 
 
    //Page.ClientScript.RegisterStartupScript(this.GetType(), "nope1", "alert('"+msg+"!')", true); 
 
    //Page.ClientScript.RegisterStartupScript(this.GetType(), "nope2", "alert('" + msg + "')", true); 
 
}

+0

所以啓動腳本的數量可以註冊爲1,並且如何調用多個函數的方式是將它們鏈接成一個字符串? 只是一個音符味精沒有聲明在第二個如果陳述。 //是有效的。非常感謝你。 – Bendom