【发布时间】:2009-09-08 04:35:31
【问题描述】:
由于我是一名 Windows 程序员,因此在表单上显示消息框非常容易。
但是在ASP.NET 页面上我不知道如何显示它?
实际上我有一些条件,基于此我想向用户显示一个消息框以获取他/她的响应,并基于该响应我想继续。 例如我想问用户“你想继续吗?”有两个按钮“是”和“否”。
【问题讨论】:
标签: asp.net
由于我是一名 Windows 程序员,因此在表单上显示消息框非常容易。
但是在ASP.NET 页面上我不知道如何显示它?
实际上我有一些条件,基于此我想向用户显示一个消息框以获取他/她的响应,并基于该响应我想继续。 例如我想问用户“你想继续吗?”有两个按钮“是”和“否”。
【问题讨论】:
标签: asp.net
您可以使用JavaScript 执行此操作。在你的代码中包含这个 sn-p -
<script type='text/javascript'>
document.getElementById("someButtonId").onclick = function() {
var confirmation = window.confirm("Are you sure?"); //confirmation variable will contain true/false.
if(confirmation) { /* Write code for Yes */ }
else { /* Write code for No */ }
}
</script>
【讨论】:
显示 Yes No 对话框的唯一方法是设计一个自定义对话框(Javascript 确认只能产生 OK 和 Cancel)。
幸运的是,ASP.NET Ajax 控件 (Ajaxcontroltoolkit) 使这项工作变得简单,因为您可以将面板作为带有所需按钮的消息框,并使用 ModalPopupExtender 来模拟对话框。
编辑:
对于您使用 javascript 提出的问题,您可以做到(这是一个比目前所见的任何解决方案都简单得多的解决方案),但准备只有 OK 和 Cancel 作为两个可能的答案。 UI设计师的噩梦! :(
基本上,在您的 aspx 页面中为该按钮或其他任何内容提供以下两个属性:
onClientClick = "javascript:confirm('you sure you wanna do this?');" onClick="myButton_Click"
只有在消息对话框上按下 OK 时才会运行 onClick。
【讨论】:
window.alert();窗口.确认();和 window.prompt(); 我猜这就是你要找的东西。
【讨论】:
你可以使用
window.confirm
为此。
它显示一个带有消息和两个按钮的模式对话框,OK 和 Cancel。
例如:
if (window.confirm("Want to see my mood ring?"))
{
// wants to continue
}
else
{
// cancel the action
}
编辑:
您还可以使用 jQuery 开发自定义消息框。这是一个不错的
【讨论】:
.aspx 标记
<head runat="server">
<title>Sample Page</title>
<script type="text/javascript">
function doSubmit(){
var confirmation = window.confirm("Are you sure?");
document.getElementById("HiddenField1")["value"]=confirmation;
return true;
}
</script>
</head>
<body>
<form id="form1" runat="server" onsubmit="return doSubmit()" >
<div>
<asp:HiddenField ID="HiddenField1" runat="server" />
<asp:Button ID="Button1" runat="server" Text="Button" />
</div>
</form>
</body>
代码隐藏
protected void Page_Load(object sender, EventArgs e)
{
if (HiddenField1.Value == "true")
{
}
else
{
}
}
【讨论】:
如果您真的想要“是”/“否”按钮(或任何不是您的标准确定/取消按钮),您可以执行以下操作:
主页:
<html>
<body>
<script>
function ShowYesNo() {
var answer = window.showModalDialog("myModalDialog.htm", '', "dialogWidth:300px; dialogHeight:200px; center:yes");
document.write('Clicked yes');
} else {
document.write('Clicked no');
}
}
ShowYesNo();
</script>
</body>
</html>
MyModalDialog.htm
<html>
<body>
<p> Do you want to proceed?" </p>
<input type = "button" id = "buttonYes" value = "Yes" onclick = "buttonOnClick('yes')">
<input type = "button" id = "buttonNo" value = "No" onclick = "buttonOnClick('no')">
<script type = "text/javascript">
function buttonOnClick(message) {
window.returnValue = message;
window.close();
}
</script>
</body>
</html>
【讨论】:
您可以使用确认JavaScript 功能,但仅限于确定/取消选项。如果这不是您想要的,您可以依靠 VBScript MsgBox 客户端功能。请记住,这样做仅适用于 Internet Explorer。
function PerformDelete(id)
{
if(confirm("I am about to delete this record. Is this ok?"))
{
//your code here
}
}
【讨论】:
我有一个解决方案,可能对你有帮助,在 Asp.Net 中使用 C# 的相同消息框或构造对话框,首先你应该添加命名空间,
使用 System.Windows.Forms;
那么,在你想调用消息框或构造对话框的地方,你可以像在c#中那样简单地调用它,比如:
DialogResult dialogResult = MessageBox.Show("Are you shure?", "Some Title", MessageBoxButtons.YesNo);
if (dialogResult == DialogResult.Yes) { Response.Redirect("Page.aspx"); } else if (dialogResult == DialogResult.No) { MessageBox.Show("You Select Nothing to do... :("); }
我想,我解释得很好,如有错误请见谅....
【讨论】: