代码改变世界

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

2010-04-27 16:07  Virus-BeautyCode  阅读(910)  评论(0编辑  收藏  举报

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

 

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="OrderStatus.aspx.cs" Inherits="KB.DSN.Web.Order.OrderStatus" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>

    <script type="text/javascript">
        function trytry() {
           //if (confirm("确定要退出吗?"))
                window.opener = null;
                window.close();
        }

    </script>

</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:Label ID="lblMsg" runat="server" Text="Label"></asp:Label>
        <asp:Button ID="btnClose" runat="server" Text="关闭页面" OnClientClick="trytry(); " />
    </div>
    </form>
</body>
</html>

 

 

 

 

   在网页制作中经常出现是否确认按钮,特别是在删除数据库是,如果没有做这种设置就会引起数据的丢失。如果做了确认按钮后就会给用户一次补救的机会,这样就避免了不必要的数据丢失。如果直接用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";
        
        }

    }