我在 asp.net Web 应用程序中使用以下消息框。现在我想将此消息框转换为确认消息框,并在它为真时执行某些操作,否则意味着拒绝该应用程序。
ScriptManager.RegisterStartupScript(this, this.GetType(), "temp", "<script language='javascript'>alert('Are you sure, you want to apply?');</script>", false);
我认为你正在以错误的方式解决这个问题。您应该在发回之前显示此确认信息,然后仅在他们选择“应用”时才发回。
使用 ASP.NET Web 控件,该Button
控件具有OnClientClick
可用于在 Http POST 之前调用 javascript 的属性:
你可以这样做:
<asp:button id="btn"
runat="server"
Text="Apply"
OnClientClick="return confirm('Are you sure you wish to apply?');"
OnClick="btn_Click" />
在下面注册脚本而不是警报
<script type="text/javascript">
var r=confirm("Are you sure you are sure?")
if (r==true)
{
//You pressed OK!
}
else
{
//You pressed Cancel!
}
</script>
与确认框等效的javascript 是confirm() 方法。此方法根据用户的“确定”或“取消”按钮响应返回真或假值。
用法:
var confirmed = confirm('Are you sure?','Are you sure you want to delete this item?');
if(confirmed){
//do something
} else {
//do something else
}
试试这个
将此添加到您的 cs 文件以显示 aconfirm
而不是alert
string confirm =
"if(confirm('Are you surely want to do this ??')) __doPostBack('', 'confirmed');";
ScriptManager.RegisterStartupScript(this, this.GetType(), "temp", confirm, true);
在同一页面上添加此内容以检查用户何时来自该确认框
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
}
}
对于我使用的这个,__doPostBack
你可以从这里了解更多信息。希望它会帮助你
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();"/>
private void showMessage(string msg){
ScriptManager.RegisterStartupScript(this, this.GetType(), "temp", "<script language='javascript'>alert('"+ msg +"');</script>", false);
受保护的无效BtnReg_Click(对象发送者,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();
}
}
要在 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.
}
这会从网页向用户显示“确定/取消”对话框。如果用户单击“确定”,则事件命令中的函数将触发。如果用户单击“取消”,则不会发生任何事情。
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;
}
}