我的程序也有这个问题,当用户提交后,后台需要执行时间比较长的时候,如果点击两次,程序会执行两次。 初步想法是写一个自定义的服务器控件按钮,自己定义服务器端点击事件,在 主要代码为: writer.AddAttribute("OnClick","JavaScript:" + Page.GetPostBackEventReference(this)+";this.disabled='disabled'"); 这样利用_doPostBack()来提交页面,提交后按钮变灰不能用。当页面刷新后,按钮自动恢复成可用状态. using System; using System.Web.UI; using System.Web.UI.WebControls; using System.ComponentModel;namespace test {
/// <summary> /// WebCustomControl1 的摘要说明。 /// </summary> [DefaultProperty("Text"), ToolboxData("<{0}:WebCustomControl1 runat=server></{0}:WebCustomControl1>")] public class WebCustomControl1 : System.Web.UI.WebControls.WebControl,System.Web.UI.IPostBackEventHandler { private string text; public event System.EventHandler OnClick2; [Bindable(true), Category("Appearance"), DefaultValue("")] public string Text { get { return text; } set { text = value; } } /// <summary> /// 将此控件呈现给指定的输出参数。 /// </summary> /// <param name="output"> 要写出到的 HTML 编写器 </param> protected override void Render(HtmlTextWriter output) { output.AddAttribute("type","button"); output.AddAttribute("name",this.UniqueID); output.AddAttribute("value",this.text); output.AddAttribute("onclick","javascript:"+Page.GetPostBackEventReference(this)+";this.disabled='disabled'"); output.RenderBeginTag("input"); output.RenderEndTag();
呵呵
初步想法是写一个自定义的服务器控件按钮,自己定义服务器端点击事件,在
主要代码为:
writer.AddAttribute("OnClick","JavaScript:" + Page.GetPostBackEventReference(this)+";this.disabled='disabled'");
这样利用_doPostBack()来提交页面,提交后按钮变灰不能用。当页面刷新后,按钮自动恢复成可用状态.
using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.ComponentModel;namespace test
{
/// <summary>
/// WebCustomControl1 的摘要说明。
/// </summary>
[DefaultProperty("Text"),
ToolboxData("<{0}:WebCustomControl1 runat=server></{0}:WebCustomControl1>")]
public class WebCustomControl1 : System.Web.UI.WebControls.WebControl,System.Web.UI.IPostBackEventHandler
{
private string text;
public event System.EventHandler OnClick2;
[Bindable(true),
Category("Appearance"),
DefaultValue("")]
public string Text
{
get
{
return text;
} set
{
text = value;
}
} /// <summary>
/// 将此控件呈现给指定的输出参数。
/// </summary>
/// <param name="output"> 要写出到的 HTML 编写器 </param>
protected override void Render(HtmlTextWriter output)
{
output.AddAttribute("type","button");
output.AddAttribute("name",this.UniqueID);
output.AddAttribute("value",this.text);
output.AddAttribute("onclick","javascript:"+Page.GetPostBackEventReference(this)+";this.disabled='disabled'");
output.RenderBeginTag("input");
output.RenderEndTag();
}
#region IPostBackEventHandler 成员 public void RaisePostBackEvent(string eventArgument)
{
// TODO: 添加 WebCustomControl1.RaisePostBackEvent 实现
if(this.OnClick2!=null)
{
this.OnClick2(this,System.EventArgs.Empty);
}
} #endregion
}
}