asp.net中弹出确认窗口(confirm),实现删除确认的功能

在网页制作中经常出现是否确认按钮,特别是在删除数据库是,如果没有做这种设置就会引起数据的丢失。如果做了确认按钮后就会给用户一次补救的机会,这样就避免了不必要的数据丢失。如果直接用js写的话有很难和后台的操作联系。
解决方案:
           给按钮添加Attributes属性,即Button1.Attributes["OnClick"] = "return confirm('are you sure?')";
这样在客户端生成 OnClick="return confirm('are you sure?')" 用户执行按钮的操作时,先在本地执行弹出一个confirm的确认窗口,再根据用户的选择,判断是否要执行按钮的操作。可能在刚开始的时候会认为服务器端是怎么知道用户的选择,其实在点击后,当选择“取消”时客户端自己进行确认,并没有发到服务器端进行确认。
下面就是一个例子
.aspx代码
<form id="Form1" method="post" runat="server">
            
<FONT face="宋体">
                
<asp:Button id="Button1" runat="server" Text="Button"></asp:Button>
                
<asp:Label id="Label1" runat="server">Label</asp:Label></FONT>
        
</form>

.cs代码
private void Page_Load(object sender, System.EventArgs e)
        
{
            
// 在此处放置用户代码以初始化页面
            Button1.Attributes["OnClick"= "return confirm('are you sure?')";
            Label1.Text
="are you sure";
        }


private void Button1_Click(object sender, System.EventArgs e)
        
{
            Label1.Text
="I'm sure";
        
        }

    }
posted @ 2008-04-08 11:29  牵牛望岳  阅读(269)  评论(0编辑  收藏  举报