[英]How to use Message Box in asp.net
我试图向用户显示一个消息框,他们可以从消息框中选择是或否; 如果他们选择 Yes 我想调用某个函数,如果他们选择 NO 那么我想调用另一个函数。 这不是我现在在我的代码中所拥有的,请帮忙。 谢谢这是我的代码:
protected void TEST()
{
string ID= ddlPr.SelectedValue;
string PRACTICE_TYPE = DDL_TYPE.SelectedValue;
using (SqlConnection con = new SqlConnection(strConnString))
{
using (SqlCommand cmd = new SqlCommand())
{
cmd.Connection = con;
cmd.CommandType = CommandType.Text;
cmd.CommandText = "SELECT count(*) from [DA2].[QPMS].[QPMS_RESPONSE] WHERE ID=@ID";
cmd.Parameters.AddWithValue("@ID", ID);
con.Open();
int result = (int)cmd.ExecuteScalar();
if (result >=1)
{
//if the user selects YES i want to call some function if user selects No then i want to call another function.
string myscript = "alert ('message');";
Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "MyScript", myscript, true);
}
con.Close();
}
}
}
<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" runat="Server">
<asp:Button ID="Button3" runat="server" Style="position: static" Text="Button" OnClick="Button1_Click" />
<asp:Button ID="Button2" runat="server" CausesValidation="False" OnClick="Button2_Click" Style="position: static; display: none" Text="Ok" />
<asp:Button ID="Button5" runat="server" CausesValidation="False" OnClick="Button5_Click" Style="position: static; display: none" Text="Cancel" />
<script type="text/javascript">
function confirmProcess()
{
if (confirm('are you sure to continue?'))
{
//if you are missing this, always use ClientID to get reference to button //control when used with master pages
document.getElementById('<%=Button2.ClientID%>').click();
}
else
{
document.getElementById('<%=Button5.ClientID%>').click();
}
}
</script>
</asp:Content>
//代码隐藏
protected void Button5_Click(object sender, EventArgs e)
{
Response.Write("Clicked Canceled");
}
protected void Button2_Click(object sender, EventArgs e)
{
Response.Write("Clicked OK");
}
protected void Button1_Click(object sender, EventArgs e)
{
String csname = "PopupScript";
Type cstype = this.GetType();
ClientScriptManager cs = Page.ClientScript;
if (!cs.IsStartupScriptRegistered(cstype, csname))
{
String cstext = "confirmProcess();";
cs.RegisterStartupScript(cstype, csname, cstext, true);
}
}
我目前在我所有的 Web 应用程序中都使用它我创建了一个静态方法,我可以调用 2 个重载方法之一,这个方法 100% 我刚刚测试确认
public static void ShowClientMessageDlg(string msg, string aValue, string redirect = "")
{
string msgFormat;
msgFormat = string.Format(" {0}", aValue);
msg = msg + msgFormat;
HttpContext.Current.Response.Write("<script type='text/javascript'>alert('" + msg + "');</script>");
}
/// <summary>
/// Joins a String of Values to create a Message Box
/// </summary>
/// <param name="msg"></param>
/// <param name="aValue"></param>
/// <param name="redirect"></param>
public static void ShowClientMessageDlg(string msg, List<string> aValue, string redirect = "")
{
string msgFormat;
msgFormat = string.Format(" {0}", string.Join<string>(", ", aValue.ToArray()));
msg = msg + msgFormat;
HttpContext.Current.Response.Write("<script type='text/javascript'>alert('" + msg + "');</script>");
}
正如 Rahul 在评论中所说,消息框仅在 winforms 中可用。 此外, javascript alert
只会显示一条消息。 您不能根据最终用户的响应执行不同的代码块。 你想要的是一个确认对话框。 这里有一个简单的例子。
来自链接:
<input type=button value="Try it now"
onClick="if(confirm('Format the hard disk?'))
alert('You are very brave!');
else alert('A wise decision!')">
对于asp,只需创建一个不同的函数来调用而不是警报。
如果您想要 javascript 的替代品,您还可以使用AjaxToolkit ConfirmButton Extender 。 它基于单击按钮,因此您必须“作弊”才能使用它。 调用MyButton.Click()
方法以激活确认对话框。 您可能可以通过设置Display: none;
来隐藏按钮Display: none;
.
总的来说,我认为 javascript 仍然可能是你最好的选择。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.