我正在尝试向用户显示一个消息框,他们可以从消息框中选择YES或NO;如果他们选择是我想要调用某个函数,如果他们选择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();
}
}
}
答案 0 :(得分:0)
<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);
}
}
答案 1 :(得分:0)
我目前在我的所有Web应用程序中使用它我创建了一个静态方法,我可以调用其中一个重载方法,这个方法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>");
}
答案 2 :(得分:0)
正如Rahul在评论中所述,消息框仅在winforms中可用。此外,javascript alert
只会显示一条消息。您无法根据最终用户响应执行不同的代码块。你想要的是一个确认对话框。这是一个简单的例子here.
来自链接:
<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。它基于按钮点击,因此您必须&#34;作弊&#34;一点点使用它。调用MyButton.Click()
方法以激活确认对话框。您可以通过设置Display: none;
来隐藏按钮。
总的来说,我认为javascript仍然是你最好的选择。