C# Web应用程序中的确认消息框
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11861271/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Confirmation message box in webapplication
提问by Milton Fernando
Am using the below message box in asp.net web application. Now i want to convert this message box as a confirmation message box and do something when it is true else means reject the application.
我在 asp.net web 应用程序中使用以下消息框。现在我想将此消息框转换为确认消息框,并在为真时执行某些操作,否则表示拒绝该应用程序。
ScriptManager.RegisterStartupScript(this, this.GetType(), "temp", "<script language='javascript'>alert('Are you sure, you want to apply?');</script>", false);
采纳答案by Curt
I think you are going about this the wrong way. You should display this confirmation beforeposting back, and then only post back if they choose to "Apply".
我认为你这样做是错误的。您应该在回发之前显示此确认信息,然后仅当他们选择“申请”时才回发。
Using ASP.NET web controls, the Buttoncontrol has an OnClientClickproperty which can be used to call javascript prior to Http POST:
使用 ASP.NET Web 控件,该Button控件具有一个OnClientClick属性,可用于在 Http POST 之前调用 javascript:
You could do something like this:
你可以这样做:
<asp:button id="btn"
runat="server"
Text="Apply"
OnClientClick="return confirm('Are you sure you wish to apply?');"
OnClick="btn_Click" />
回答by Arsen Mkrtchyan
register script bellow instead of alert
在下面注册脚本而不是警报
<script type="text/javascript">
var r=confirm("Are you sure you are sure?")
if (r==true)
{
//You pressed OK!
}
else
{
//You pressed Cancel!
}
</script>
回答by Clayton
The javascript equivalent to a confirmation box is the confirm() method. This method returns a true or false value depending on the user's "OK" or "Cancel" button response.
与确认框等效的 javascript 是 confirm() 方法。此方法根据用户的“确定”或“取消”按钮响应返回 true 或 false 值。
Usage:
用法:
var confirmed = confirm('Are you sure?','Are you sure you want to delete this item?');
if(confirmed){
//do something
} else {
//do something else
}
回答by yogi
Try this
尝试这个
Add this on your cs file to display a confirminstead of alert
将此添加到您的 cs 文件中以显示confirm而不是alert
string confirm =
"if(confirm('Are you surely want to do this ??')) __doPostBack('', 'confirmed');";
ScriptManager.RegisterStartupScript(this, this.GetType(), "temp", confirm, true);
Add this on same page to check when user is coming from that confirmation box
在同一页面上添加此项以检查用户何时来自该确认框
protected void Page_Load(object sender, EventArgs e)
{
string parameter = Request["__EVENTARGUMENT"];
if (string.Equals("confirmed",
parameter,
StringComparison.InvariantCultureIgnoreCase))
{
// Call your server side method here
}
}
For this I used __doPostBackyou can learn more about it from here.
Hope it'll help you
为此,我使用了__doPostBack您可以从这里了解更多信息。希望它会帮助你
回答by Krunal Mevada
try this code check on OnClientClickevent when user click on button
OnClientClick当用户单击按钮时,尝试使用此代码检查事件
<script type="text/javascript">
function check()
{
var chk =confirm("Are you sure you are sure?")
if (chk==true)
{
// try you want
}
else
{
// try you do not want
}
return true;
}
</script>
<asp:button id="Button1"
runat="server"
Text="Button1"
OnClientClick="return check();"/>
回答by ZumuruDa ALi
private void showMessage(string msg){
ScriptManager.RegisterStartupScript(this, this.GetType(), "temp", "<script language='javascript'>alert('"+ msg +"');</script>", false);
protected void BtnReg_Click(object sender, EventArgs e) {
protected void BtnReg_Click(object sender, EventArgs e) {
OracleHelper.OracleDBOpen();
object flag = OracleHelper.OracleExecuteScalar("your select Query ");
if (flag == null)
{
showMessage("Failed !!! ");
}
else
{
string reg = String.Format("your Insert Query ");
showMessage("successfuly");
OracleHelper.OracleExecuteNonQuery(reg);
}
OracleHelper.OracleDBClose();
}
}
回答by David T.
To do this completely within C#, you can try this:
要在 C# 中完全做到这一点,你可以试试这个:
protected override void OnInit(EventArgs e)
{
AddConfirmationButton();
base.OnInit(e);
}
private void AddConfirmationButton()
{
Button confirmButton = new Button();
confirmButton.Text = "Action Foo";
string confirmationMessage = "Are you sure you wish to do action Foo?";
confirmButton.OnClientClick = "return confirm('" + confirmationMessage + "');";
confirmButton.Command += confirmButton_Command;
Controls.Add(confirmButton);
}
void confirmationMessage_Command(object sender, CommandEventArgs e)
{
DoActionFoo(); //work your magic here.
}
This presents and "OK/Cancel" dialog box to the user from the webpage. If the user clicks 'ok', the function from the command Event fires. If the user clicks 'cancel', nothing happens.
这会从网页向用户显示“确定/取消”对话框。如果用户单击“确定”,则命令事件中的函数将触发。如果用户单击“取消”,则什么也不会发生。
回答by Dinima
ScriptManager.RegisterStartupScript(page,this.GetType(), "temp","javascript:calopen();
",true);
function calopen()
{
if (confirm("Are you sure?"+'\n'+"Are you want to delete"))
{
enter code here
}
else
{
return false;
}
}

