如何从asp.net中的代码调用确认消息?

嗨,我想从asp.net中的代码后面调用客户端javascript确认消息。

我想使用确认消息中的true或false返回值。

我这样做,但这不是正确的方法,请告诉我我该怎么做。

ScriptManager.RegisterStartupScript(this, this.GetType(), "myconfirm", "confirm('No Rule Type Ids found for This Rule.');", true); 

我想这就是你想要实现的目标:

  

.aspx代码

  

C#

 public void OnConfirm(object sender, EventArgs e) { string confirmValue = Request.Form["confirm_value"]; if (confirmValue == "Yes") { this.Page.ClientScript.RegisterStartupScript(this.GetType(), "alert", "alert('You clicked YES!')", true); } else { this.Page.ClientScript.RegisterStartupScript(this.GetType(), "alert", "alert('You clicked NO!')", true); } } 

而不是在后面的代码中直接写入确认。 写下javascript函数的名称。 例如,

 ScriptManager.RegisterStartupScript(this, this.GetType(), "myconfirm", "OpenConfirmDialog();", true); 

在你的javascript中,编写函数OpenConfirmDialog

  

您不能将客户端代码与服务器端代码混合使用。 在服务器端代码完成之前,客户端代码(javascript)将不会发送到客户端(浏览器)。

您需要停止处理并向用户显示问题(可能通过重定向到其他页面),然后(确认)继续处理。

 Response.Write(""); 

使用:

 Response.Write(""); 

你可以不使用Javascript函数来做到这一点

尝试

 if (MessageBox.Show("confirm('No Rule Type Ids found for This Rule.')", "myconfirm", MessageBoxButtons.YesNo) == DialogResult.Yes) { // yes } else { //No }