[英]JavaScript code behind in c#
我的cs文件中包含以下代碼。
private bool ValidateFirst()
{
StringBuilder sb2 = new StringBuilder();
sb2.Append(@"<script language='javascript'> var bContinue = true;");
sb2.Append(@"bContinue = confirm(");
sb2.Append("'some text here'");
sb2.Append(@");");
sb2.Append("if (bContinue) {");
sb2.Append(this.Page.ClientScript.GetPostBackEventReference(this, "OKFinal"));
sb2.Append("}</script>");
this.Page.ClientScript.RegisterStartupScript(this.Page.GetType(), "ConfirmDialog", sb2.ToString());
return false;
}
所以我想要在這里顯示一個確認彈出頁面。 當我在JSFiddle http://jsfiddle.net/2rbcsvh6/上測試sb2.ToString()
時,它可以工作(_doPostBack事件不起作用),但在我的應用程序中卻無法顯示。 該代碼位於屏幕控制器操作命令中。
protected void screenController_ActionCommand(object sender, Web.Controls.CancelCommandEventArgs e)
{
if (e.CommandName == ScreenController.OkCommand)
{
// this is the method that contains the script
if (ValidateFirst)
{
this.Save();
}
}
}
問題是當您嵌入JS時,頁面已被加載並呈現。 您需要在頁面加載之前運行代碼,即Page_Load
或者將JS包裝到一個函數中,然后可以從客戶端調用該函數。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.