方法一:
只在按钮前台代码中增加OnClientClick="this.disabled=true;" UseSubmitBehavior="False"即可。
方法二:
protected
void
Page_Load(
object
sender, EventArgs e)
{
Button1.Attributes.Add( " onclick " , this .GetPostBackEventReference(Button1) + " ;this.disabled=true; " );
}
{
Button1.Attributes.Add( " onclick " , this .GetPostBackEventReference(Button1) + " ;this.disabled=true; " );
}
注:不要写在if (!Page.IsPostBack) 里,否则会脚本错误的。
过程就是单击按钮后将其置为disabled,即this.disabled=true。
虽然过程很简单,但是里面有个小技巧(我以前也不知道),就是Button控件里不常用的UseSubmitBehavior属性,要使Button单击后disabled,并且页面(form)还要提交,就必须把UseSubmitBehavior设为false,否则页面(form)将不会提交。
MSDN对UseSubmitBehavior的解释是:
使用 UseSubmitBehavior 属性来指定 Button 控件使用客户端浏览器的提交机制还是 ASP.NET 回发机制。默认情况下,此属性的值为 true ,从而导致 Button 控件使用浏览器的提交机制。如果指定为 false ,则 ASP.NET 页框架将客户端脚本添加到页面,以将窗体发送到服务器。
当 UseSubmitBehavior 属性为 false 时,控件开发人员可以使用 GetPostBackEventReference 方法来返回 Button 的客户端回发事件。GetPostBackEventReference 方法返回的字符串包含客户端函数调用的文本,可以插入到客户端事件处理程序中。
使用 UseSubmitBehavior 属性来指定 Button 控件使用客户端浏览器的提交机制还是 ASP.NET 回发机制。默认情况下,此属性的值为 true ,从而导致 Button 控件使用浏览器的提交机制。如果指定为 false ,则 ASP.NET 页框架将客户端脚本添加到页面,以将窗体发送到服务器。
当 UseSubmitBehavior 属性为 false 时,控件开发人员可以使用 GetPostBackEventReference 方法来返回 Button 的客户端回发事件。GetPostBackEventReference 方法返回的字符串包含客户端函数调用的文本,可以插入到客户端事件处理程序中。
例如
<
asp:Button
ID
="doPublishButton"
runat
="server"
Text
="发布"
OnClick
="doPublishButton_Click"
Height ="30px" OnClientClick ="this.disabled=true;" UseSubmitBehavior ="False" />
Height ="30px" OnClientClick ="this.disabled=true;" UseSubmitBehavior ="False" />
当UseSubmitBehavior为false时,会在输出的Html里看到
<
input
type
="button"
name
="Issue1:doPublishButton"
value
="发布"
onclick
="this.disabled=true;__doPostBack('Issue1$doPublishButton','')"
language
="javascript"
id
="Issue1_doPublishButton"
style
="height:30px;"
/>
__doPostBack('Issue1$doPublishButton',''),就是asp.net加上去的。如果UseSubmitBehavior为true,就不会有这句话,页面(form)就不会提交。
转自:http://mm.ala.blog.163.com/blog/static/30516034201092610176779/
备注:只在按钮前台代码中增加OnClientClick="this.disabled=true;" UseSubmitBehavior="False"即可。