Confirm对话框在数据库操作前的弹出
前两天在修改项目代码中的一个问题时,老大提出了一个新的需求,就是在数据库的操作前检测一下数据库中的数据是否符合某种条件,如果符合弹出Confirm对话框,如果操作者确认的话点击OK继续执行,点击取消的话则后面的操作不执行。这个问题让我想了好久,在网上找了很多,最终用两个Button来实现这个功能 。一个按钮是不显示在页面上的,该按钮的单击事件里就写单击Confirm确认以后要执行的代码,而显示的那个按钮的事件代码只写到弹出Confirm那里。最终效果是实现了,但是这个方法有个缺点,就是每当你单击按钮的时候,这个单击事件一定要先走一遍,把后台的脚本注册一次。
if (!IsPostBack)
{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//将按钮的属性设置为不显示,如果隐藏就会出错,HTML解析不到这个控件,事件也会不执行
Button2.Attributes.Add("style", "display:none");
}
protected void Button1_Click(object sender, EventArgs e)
{
//假设条件
if (this.TextBox1.Text == "ww")
{
//注册脚本
Page.ClientScript.RegisterStartupScript(this.GetType(), "test", "<script language=javascript>if(confirm('wwwww'==true))document.all.Button2.click();</script>");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
}
}
//确认后执行的事件
protected void Button2_Click(object sender, EventArgs e)
{
Response.Write("This is test");
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)