我正在开发asp.net web应用程序,因为我需要使用服务器端是/否消息框,我搜索了所有的地方,但我还没有找到一个好的解决方案,
我发现只有客户端javascript是/否消息框,如下面的代码
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title></title>
<script type = "text/javascript">
function Confirm() {
var confirm_value = document.createElement("INPUT");
confirm_value.type = "hidden";
confirm_value.name = "confirm_value";
if (confirm("Do you want to save data?")) {
confirm_value.value = "Yes";
} else {
confirm_value.value = "No";
}
document.forms[0].appendChild(confirm_value);
}
</script>
</head>
<body>
<form id="form1" runat="server">
<asp:Button ID="btnConfirm" runat="server" OnClick = "OnConfirm" Text = "Raise Confirm" OnClientClick = "Confirm()"/>
</form>
</body>
</html>
代码背后:
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);
}
}
但这个解决方案没有帮助我,我需要在asp.net c#中完全服务器端是/否消息框。我搜索&amp;尝试了下面的代码,但它部分执行。
protected void btnSave_Click(object sender, EventArgs e)
{
System.Text.StringBuilder sb = new System.Text.StringBuilder();
sb.Append("<script type = 'text/javascript'>");
sb.Append("window.onload=function(){");
sb.Append("confirm('");
sb.Append("Hi");
sb.Append("')};");
sb.Append("</script>");
ClientScript.RegisterClientScriptBlock(this.GetType(), "confirm", sb.ToString());
string msg = "Do you want to save data?";
ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "alertMessage", "confirm('" + msg + "')", true);
}
在上面的代码中,我需要点击是选项时需要去 是阻止/当我们点击否选项时,它需要移动没有阻止。
我不知道该怎么做.. 我试过下面的代码
protected void btnSave_Click(object sender, EventArgs e)
{
System.Text.StringBuilder sb = new System.Text.StringBuilder();
sb.Append("<script type = 'text/javascript'>");
sb.Append("window.onload=function(){");
sb.Append("confirm('");
sb.Append("Hi");
sb.Append("')};");
sb.Append("</script>");
ClientScript.RegisterClientScriptBlock(this.GetType(), "confirm", sb.ToString());
string msg = "Do you want to save data?";
ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "alertMessage", "confirm('" + msg + "')", true);
string confirmValue = Request.Form["confirm"];
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);
}
}
任何身体帮助我......
答案 0 :(得分:1)
将自定义确认放入客户端代码(javascript + html Modal)
然后只需从C#代码隐藏
调用javascript函数<a href="https://jsfiddle.net/KyleMit/0fscmf3L/">Example</a>