防止ASP.NET按钮多次提交的办法
方法一
<asp:Button ID="btnSumbit" runat="server" UseSubmitBehavior="false" OnClientClick="this.value='Sumbit';this.disabled=true; " Text="Sumbit" OnClick="btnSumbit_Click" />
方法二
服务器端代码,故意让其延时等待3秒后再输入,以模拟数据库操作等慢速动作。
<asp:Button ID="btnSumbit" runat="server" UseSubmitBehavior="false" OnClientClick="this.value='Sumbit';this.disabled=true; " Text="Sumbit" OnClick="btnSumbit_Click" />
方法二
 1 <html xmlns="http://www.w3.org/1999/xhtml">
<html xmlns="http://www.w3.org/1999/xhtml">
2 <head runat="server">
<head runat="server">
3 <title>禁止多次提交网页测试</title>
    <title>禁止多次提交网页测试</title>
4 <style type="text/css">
    <style type="text/css">
5 .disable
    .disable
6 {
    {
7 border-style:none;
        border-style:none; 
8 border-width: thin;
        border-width: thin; 
9 background-color:Transparent;
        background-color:Transparent; 
10 color: #CCCCCC;
        color: #CCCCCC; 
11 cursor:wait;
        cursor:wait;
12 }
    }
13 </style>
    </style>
14 <script type="text/javascript" language="javascript">
    <script type="text/javascript" language="javascript">
15 function DisableButton()
    function DisableButton()
16 {
    {
17 document.getElementById("Button2").className  = "disable";
        document.getElementById("Button2").className  = "disable";
18 document.getElementById("Button2").value = '正在提交
        document.getElementById("Button2").value = '正在提交 .';
.';
19 document.getElementById("Button2").onclick=Function("return false;");
        document.getElementById("Button2").onclick=Function("return false;");
20 return true;
        return true;
21 }
    }
22 document.onkeydown=mykeydown;
    document.onkeydown=mykeydown;   
23 function   mykeydown()
    function   mykeydown()
24 {
    {   
25 if(event.keyCode==116) //屏蔽F5刷新键
        if(event.keyCode==116) //屏蔽F5刷新键   
26 {
        {   
27 window.event.keyCode=0;
            window.event.keyCode=0;   
28 return   false;
            return   false;   
29 }
        }   
30 }
    }   
31 </script>
    </script>
32
33 </head>
</head>
34 <body>
<body>
35 <form id="form1" runat="server">
    <form id="form1" runat="server">
36 <div>
    <div>
37 输入一些内容<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
        输入一些内容<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
38 <br />
        <br />
39 <asp:ListBox ID="ListBox1" runat="server" Height="77px" Width="332px">
        <asp:ListBox ID="ListBox1" runat="server" Height="77px" Width="332px">
40 </asp:ListBox><br />
    </asp:ListBox><br />
41 <asp:Button ID="Button2" runat="server" Text="OK" Width="77px"
        <asp:Button ID="Button2" runat="server" Text="OK" Width="77px"
42 onclick="Button2_Click" />
            onclick="Button2_Click" />
43 </div>
    </div>
44 
    
45 </form>
    </form>
46 </body>
</body>
47 </html>
</html>
48
 <html xmlns="http://www.w3.org/1999/xhtml">
<html xmlns="http://www.w3.org/1999/xhtml">2
 <head runat="server">
<head runat="server">3
 <title>禁止多次提交网页测试</title>
    <title>禁止多次提交网页测试</title>4
 <style type="text/css">
    <style type="text/css">5
 .disable
    .disable6
 {
    {7
 border-style:none;
        border-style:none; 8
 border-width: thin;
        border-width: thin; 9
 background-color:Transparent;
        background-color:Transparent; 10
 color: #CCCCCC;
        color: #CCCCCC; 11
 cursor:wait;
        cursor:wait;12
 }
    }13
 </style>
    </style>14
 <script type="text/javascript" language="javascript">
    <script type="text/javascript" language="javascript">15
 function DisableButton()
    function DisableButton()16
 {
    {17
 document.getElementById("Button2").className  = "disable";
        document.getElementById("Button2").className  = "disable";18
 document.getElementById("Button2").value = '正在提交
        document.getElementById("Button2").value = '正在提交 .';
.';19
 document.getElementById("Button2").onclick=Function("return false;");
        document.getElementById("Button2").onclick=Function("return false;");20
 return true;
        return true;21
 }
    }22
 document.onkeydown=mykeydown;
    document.onkeydown=mykeydown;   23
 function   mykeydown()
    function   mykeydown()24
 {
    {   25
 if(event.keyCode==116) //屏蔽F5刷新键
        if(event.keyCode==116) //屏蔽F5刷新键   26
 {
        {   27
 window.event.keyCode=0;
            window.event.keyCode=0;   28
 return   false;
            return   false;   29
 }
        }   30
 }
    }   31
 </script>
    </script>32

33
 </head>
</head>34
 <body>
<body>35
 <form id="form1" runat="server">
    <form id="form1" runat="server">36
 <div>
    <div>37
 输入一些内容<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
        输入一些内容<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>38
 <br />
        <br />39
 <asp:ListBox ID="ListBox1" runat="server" Height="77px" Width="332px">
        <asp:ListBox ID="ListBox1" runat="server" Height="77px" Width="332px">40
 </asp:ListBox><br />
    </asp:ListBox><br />41
 <asp:Button ID="Button2" runat="server" Text="OK" Width="77px"
        <asp:Button ID="Button2" runat="server" Text="OK" Width="77px"42
 onclick="Button2_Click" />
            onclick="Button2_Click" />43
 </div>
    </div>44
 
    45
 </form>
    </form>46
 </body>
</body>47
 </html>
</html>48

服务器端代码,故意让其延时等待3秒后再输入,以模拟数据库操作等慢速动作。
 1 public partial class Default2 : System.Web.UI.Page
public partial class Default2 : System.Web.UI.Page
2 {
{
3 static public int count = 0;
    static public int count = 0;
4 protected void Page_Load(object sender, EventArgs e)
    protected void Page_Load(object sender, EventArgs e)
5 {
    {
6 if (!IsPostBack)
        if (!IsPostBack)
7 {
        {
8 Button2.Attributes.Add("onclick", "return DisableButton();");
            Button2.Attributes.Add("onclick", "return DisableButton();");
9 }
        }
10 }
    }
11
12 protected void Button2_Click(object sender, EventArgs e)
    protected void Button2_Click(object sender, EventArgs e)
13 {
    {
14 if (TextBox1.Text != string.Empty)
        if (TextBox1.Text != string.Empty)
15 {
        {
16 System.Threading.Thread.Sleep(3000);
            System.Threading.Thread.Sleep(3000);
17 count++;
            count++;
18 ListBox1.Items.Add(new ListItem("Hello "+TextBox1.Text + "  这是你第" + count.ToString() + "次点击   " + DateTime.Now.ToString()));
            ListBox1.Items.Add(new ListItem("Hello "+TextBox1.Text + "  这是你第" + count.ToString() + "次点击   " + DateTime.Now.ToString()));
19 TextBox1.Text = "";
            TextBox1.Text = "";
20 }
        }
21 }
    }
22 }
}
 public partial class Default2 : System.Web.UI.Page
public partial class Default2 : System.Web.UI.Page2
 {
{3
 static public int count = 0;
    static public int count = 0;4
 protected void Page_Load(object sender, EventArgs e)
    protected void Page_Load(object sender, EventArgs e)5
 {
    {6
 if (!IsPostBack)
        if (!IsPostBack)7
 {
        {8
 Button2.Attributes.Add("onclick", "return DisableButton();");
            Button2.Attributes.Add("onclick", "return DisableButton();");9
 }
        }10
 }
    }11

12
 protected void Button2_Click(object sender, EventArgs e)
    protected void Button2_Click(object sender, EventArgs e)13
 {
    {14
 if (TextBox1.Text != string.Empty)
        if (TextBox1.Text != string.Empty)15
 {
        {16
 System.Threading.Thread.Sleep(3000);
            System.Threading.Thread.Sleep(3000);17
 count++;
            count++;18
 ListBox1.Items.Add(new ListItem("Hello "+TextBox1.Text + "  这是你第" + count.ToString() + "次点击   " + DateTime.Now.ToString()));
            ListBox1.Items.Add(new ListItem("Hello "+TextBox1.Text + "  这是你第" + count.ToString() + "次点击   " + DateTime.Now.ToString()));19
 TextBox1.Text = "";
            TextBox1.Text = "";20
 }
        }21
 }
    }22
 }
} 
                    
                     
                    
                 
                    
                 
 
     
     
                
            
         浙公网安备 33010602011771号
浙公网安备 33010602011771号