首页  ·  知识 ·  
Label
      编辑:  图片来源:网络

某些处理响应时间较长,为了防止用户重复连续点击提交按钮,我们通常会在点击之后 disable 此按钮。

在 ASP.NET 2.0 中 asp:button 默认使用客户端浏览器的提交机制即呈现为 input type=submit,
如果直接设置 onclientclick="this.disabled=true" ,将无法执行提交。

解决方法:
只有设置 UseSubmitBehavior=false,按钮呈现为 input type=button,然后添加脚本

        <asp:Button ID="Button1" UseSubmitBehavior="false" runat="server" OnClientClick="this.value='submitting';this.disabled=true;" onClick="Button1_Click" Text="Test1" />

曾想当然的认为可以在 form 的 onsumit 中 disable  input type=submit
注:ASP时代更常用此方案,可以同时对一个form具有多个提交按钮作出统一的“响应”

经测试,发现虽然可以回发页面,但是却无法触发服务器端的 Click 事件。

原来,当点击 input type=submit 按钮时,如果其 disabled=false(正常状态),那么浏览器会同时提交此按钮
(Request.Form 集合中可以查询到此按钮的value),而 disabled=true(禁用),浏览不会提交此按钮的value。
而 ASP.NET 内部则检索是否有Request.Form中有对应的name匹配当然页面WebControl.UnquieID 来确定谁提交了改页面,由此引发注册的服务器事件。

Updated 已经实现 2007年7月11日
让 ASP.NET 2.0 的 Sumbit Button 支持 禁用(灰化)提交

对于,input type=button ,浏览器永远都不提交此按钮给服务器,对于此类型提交,需要有一个隐藏域
input type=hidden name=__EVENTTARGET 来存储谁提交了此页面。

完整测试代码:


<%@ Page Language="C#" %>

DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">

    protected 
void Button1_Click(object sender, EventArgs e)
    
{
        System.Threading.Thread.Sleep(
1000 * 2);
    }


    protected 
void Page_Load(object sender, EventArgs e)
    
{

    }

script>

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    
<title>Untitled Pagetitle>
    
<script type="text/javascript">
    
function chk(frm) {
        frm.Button2.disabled 
=true;
        frm.Button2.value
='submitting';
    }

    
script>
head>
<body>
    
<form id="form1" runat="server" onsubmit="chk(this)">
    
<div>
        
<asp:Button ID="Button1" UseSubmitBehavior="false" runat="server" OnClientClick="this.value='submitting';this.disabled=true;" onClick="Button1_Click" Text="Test1" />
        
<asp:Button ID="Button2" runat="server" onClick="Button1_Click" Text="Test2"  />
        
<asp:Button ID="Button3" runat="server" OnClientClick="this.value='sorry, the form cant be submitted.';this.disabled=true;" onClick="Button1_Click" Text="Test3" />
    
div>
    
form>
body>
html>

 下载

本文作者:晓风残月 来源:http://www.cnblogs.com/Jinglecat
CIO之家 www.ciozj.com 微信公众号:imciow
   
免责声明:本站转载此文章旨在分享信息,不代表对其内容的完全认同。文章来源已尽可能注明,若涉及版权问题,请及时与我们联系,我们将积极配合处理。同时,我们无法对文章内容的真实性、准确性及完整性进行完全保证,对于因文章内容而产生的任何后果,本账号不承担法律责任。转载仅出于传播目的,读者应自行对内容进行核实与判断。请谨慎参考文章信息,一切责任由读者自行承担。
延伸阅读
也许感兴趣的
我们推荐的
主题最新
看看其它的
收藏至微信