- <html xmlns="http://www.w3.org/1999/xhtml" >
- <head runat="server">
- <title>无标题页</title>
- </head>
- <body>
- <form id="form1" runat="server">
- <div>
- <asp:Button ID="btn" runat="server" OnClick="Button1_Click" Text="防止恶意刷新或多次点击按钮"
- Width="191px" /></div>
- </form>
- </body>
- </html>
- protected void Page_Load(object sender, EventArgs e)
- {
- btn.Attributes.Add("onclick", "state=true;");
- StringBuilder sb = new StringBuilder();
- sb.Append("if (!state) return;");
- sb.Append("var button=document.getElementById('btn');");
- sb.Append("button.value='Please Wait...';");
- sb.Append("document.body.style.cursor='hand';");
- sb.Append("button.disabled=true;");
- string strScript = "<script>";
- strScript = strScript + "var state=false;";
- strScript = strScript + "window.attachEvent('onbeforeunload',function(){" + sb.ToString() + "});";
- strScript = strScript + "</" + "script>";
- Page.RegisterStartupScript("onbeforeunload", strScript);
- }
- protected void btn_Click(object sender, EventArgs e)
- {
- //模拟长时间的按钮处理
- System.Threading.Thread.Sleep(2000);
- Response.Write("<script>alert('防止恶意刷新或多次点击按钮');" + "</" + "script>");
- }