如何让按钮只做1次postback

方法一

 System.Text.StringBuilder sb = new System.Text.StringBuilder();
            sb.Append("if (typeof(Page_ClientValidate) == 'function') { if (Page_ClientValidate() == false) { return false; }}");    //保证验证函数的执行
            // sb.Append("if(window.confirm('are you sure?')==false) return false;");        //自定义客户端脚本
            sb.Append("return checkvalid();disableOtherSubmit();");        // disable所有submit按钮
            sb.Append(this.GetPostBackEventReference(this.btnGo));    //用__doPostBack来提交,保证按钮的服务器端click事件执行
            sb.Append(";");
            this.btnGo.Attributes.Add("onclick", sb.ToString());
 
 
 
JS: (这个会disabled 全部submitbutton,如只想disabled当前的,上面的换成this.disabled =true)
 
 function disableOtherSubmit()
        {            var obj = event.srcElement;
            var objs = document.getElementsByTagName('INPUT');
            for(var i=0; i<objs.length; i++)
            {
                if(objsIdea.type.toLowerCase() == 'submit')
                {
                    objsIdea.disabled = true;
                }
            }    
    }

 

方法二:FlexiHR 里面的savebutton是由LinkButton 和Image 组合成的

public partial class FormButton : System.Web.UI.UserControl
{

    protected override void Render(HtmlTextWriter writer)
    {
        PostBackOptions myPostBackOptions = new PostBackOptions(this);
        //myPostBackOptions.ActionUrl=""
        //myPostBackOptions.AutoPostBack = false;
        //myPostBackOptions.RequiresJavaScriptProtocol = true;
        myPostBackOptions.PerformValidation = true;
       


        Linkbuttonsave.OnClientClick = String.Format(@"BLOCKED SCRIPTsetCommand(COMMAND_SAVE);
            if(!Save('SAVE'))
            {{
                return false;
            }}
            else
            {{
               if (typeof(selfSaveAfter) == 'function')
                  {{
                    selfSaveAfter();
                  }}
            }};{0};if(Page_IsValid){{ this.disabled =true; {1}.disabled = true;}}", this.Page.ClientScript.GetPostBackEventReference(myPostBackOptions, true), Image1.ClientID);
        //this.Page.ClientScript.GetPostBackClientHyperlink(this, this.Linkbuttonsave.ClientID, true)); --这个没有客户端检测的部分的,骗人的
        base.Render(writer);
    }

 附带一些页面生成的HTML补充
 

1)没有添加 GetPostBackEventReference 前的客户端脚本
<td width="70" class="savebtnstyle" onmouseover="this.className='savebtnstyleover'" onmouseout="this.className='savebtnstyle'" >
<a onclick="BLOCKED SCRIPTsetCommand(COMMAND_SAVE);
  if(!Save('SAVE'))
  {
      return false;
  }
  else
  {
     if (typeof(selfSaveAfter) == 'function')
        {
          selfSaveAfter();
        }
  };this.disabled =true;debugger;"
  id="ctl00_FormButton_Linkbuttonsave"
  href="BLOCKED SCRIPTWebForm_DoPostBackWithOptions(new WebForm_PostBackOptions(&quot;ctl00$FormButton$Linkbuttonsave&quot;, &quot;&quot;, true, &quot;&quot;, &quot;&quot;, false, true))"
  style="display:inline-block;color:Black;text-decoration:none;width:100%;">
  <img id="ctl00_FormButton_Image1" src="../App_Themes/Default/images/save.gif"
  align="absmiddle" style="border-width:0px;" />&nbsp;<span id="ctl00_FormButton_Label1" class="xButton">Save</span>
  </a>
 </td>
 
2)添加 GetPostBackEventReference 后的客户端脚本
<td width="70" class="savebtnstyle" onmouseover="this.className='savebtnstyleover'" onmouseout="this.className='savebtnstyle'" >
<a onclick="BLOCKED SCRIPTsetCommand(COMMAND_SAVE);
  if(!Save('SAVE'))
  {
      return false;
  }
  else
  {
     if (typeof(selfSaveAfter) == 'function')
        {
          selfSaveAfter();
        }
  };WebForm_DoPostBackWithOptions(new WebForm_PostBackOptions(&quot;ctl00$FormButton&quot;, &quot;&quot;, true, &quot;&quot;, &quot;&quot;, false, true));
  this.disabled =true;debugger;"
   id="ctl00_FormButton_Linkbuttonsave" href="BLOCKED SCRIPT
  WebForm_DoPostBackWithOptions(new WebForm_PostBackOptions(&quot;ctl00$FormButton$quot;, &quot;&quot;, true, &quot;&quot;, &quot;&quot;, false, true))" style="display:inline-block;color:Black;text-decoration:none;width:100%;"><img id="ctl00_FormButton_Image1" src="../App_Themes/Default/images/save.gif" align="absmiddle" style="border-width:0px;" />&nbsp;<span id="ctl00_FormButton_Label1" class="xButton">Save</span></a>
  </td>
 
  方法三: 利用css的expression对本身某个属性进行判断
.btngo02
{
 background-color: expression((this.JEventOnClick)?this.disabled = true:"");
}
<HR:SearchButton ID="btnGo" runat="server"  CssClass="btngo02" Text="GO" OnClientClick="BLOCKED SCRIPTthis.JEventOnClick=true;search();" />
 
注:三种办法应用的处理各有所长,我就喜欢第三种。这样处理是最秒的。
Published Thursday, October 30, 2008 9:02 AM by jawance
Filed under:

Comments

No Comments