【发布时间】:2011-05-19 16:01:29
【问题描述】:
我的问题是,我有一个简单的 Web 表单,其中包含两个文本框和一个按钮。页面上有一些 asp.net 验证器控件。所以我希望在完成所有验证后客户端禁用按钮。禁用按钮,我正在执行一些服务器端代码。所有这些都工作正常,但是,如果我设置按钮的回发 url,它会失败。下面是编码的一部分,它会给你一些简要的想法。任何提示将不胜感激.......
我想在复合控件中实现此功能。 这是按钮类
public class MyButton : Button
{
protected override void OnPreRender(EventArgs e)
{
if (this.CausesValidation)
{
if (!String.IsNullOrEmpty(this.ValidationGroup))
{
this.Attributes.Add("onclick", @"javascript:
if (typeof(Page_ClientValidate) == 'function')
{
if (Page_ClientValidate('" + this.ValidationGroup + "')){" + this.ClientID + ".disabled=true;" + Page.GetPostBackEventReference(this) +
"}else{return;}} else{" + this.ClientID + ".disabled=true;" + Page.GetPostBackEventReference(this) + "}");
}
else
{
this.Attributes.Add("onclick", @"javascript:
if (typeof(Page_ClientValidate) == 'function')
{
if (Page_ClientValidate()){" + this.ClientID + ".disabled=true;" + Page.GetPostBackEventReference(this) +
"}else{return;}} else{" + this.ClientID + ".disabled=true;" + Page.GetPostBackEventReference(this) + "}");
}
}
else
this.Attributes.Add("onclick", "javascript:" + this.ClientID + ".disabled=true;" + Page.GetPostBackEventReference(this));
base.OnPreRender(e);
}
【问题讨论】:
-
所有这些解决方案都存在一个问题,如果在系统缓慢时单击按钮的速度足够快,则可以将第二次推送排队并再次调用 on-click 方法,即使第一次点击禁用按钮。