in

SDT Community Server

SDT Forums, Blogs, Photos server.

Coolboy

Disable Button when onclicked

此处提供的代码用来实现当asp.net页面中的某个Button被点击后disable掉该页面中所有的Button,从而防止提交延时导致的多次提交。基于之前的onceclickbutton脚本.

<script language="javascript">
        
<!--
        
        
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;
                }
            }        }
            
        
//-->
        </script>
public class PreventMultiClick : System.Web.UI.Page
    {
        
protected System.Web.UI.WebControls.Button Button1;
        
protected System.Web.UI.WebControls.Button Button2;
        
protected System.Web.UI.WebControls.LinkButton LinkButton1;
        
protected System.Web.UI.WebControls.Button Button3;
    
        
private void Page_Load(object sender, System.EventArgs e)
        {
            
this.GetPostBackEventReference(this.Button3);        //保证 __doPostBack(eventTarget, eventArgument) 正确注册
            if(!IsPostBack)
            {
                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("disableOtherSubmit();");        // disable所有submit按钮
                sb.Append(this.GetPostBackEventReference(this.Button3));    //用__doPostBack来提交,保证按钮的服务器端click事件执行
                sb.Append(";");
                Button3.Attributes.Add(
"onclick",sb.ToString());
            }
        }

        
#region Web Form Designer generated code
        
override protected void OnInit(EventArgs e)
        {
            
//
            
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
            
//
            InitializeComponent();
            
base.OnInit(e);
        }
        
        
/// <summary>
        
/// Required method for Designer support - do not modify
        
/// the contents of this method with the code editor.
        
/// </summary>
        private void InitializeComponent()
        {    
            
this.Button3.Click += new System.EventHandler(this.Button3_Click);
            
this.Load += new System.EventHandler(this.Page_Load);

        }
        
#endregion

        
private void Button3_Click(object sender, System.EventArgs e)
        {
            System.Threading.Thread.Sleep(
3000);
            Response.Write(
"Hello world!");
        }
    }

此处只是disable掉所有的submit button, 我觉得其它的可提交控件也是可以通过类似的方法来disable的.
Published Mar 09 2008, 04:55 AM by Coolboy
Filed under: ,

Comments

 

jawance said:

1。并不只有button才有sumit的动作吧。

2。里面加个属性,如果sumit后作的动作成功了,恢复为

最初的状态,这样可能比较好。

3。现在只是有个window.confirm确认窗口阻隔了一下。

(这里可以让默认按钮为No)

4。一般post回去会刷新页面,用户可以通过这个确认

是否已经sumit了;如用无刷新的话,用上面的windos.confim 这样比较好。

March 10, 2008 8:53 AM
 

wego said:

开个异步线程显示提交进度岂不锦上添花 ?!

March 10, 2008 10:54 AM
 

Coolboy said:

。并不只有button才有sumit的动作吧。--->那也可以在其他控件上加上这个东东

2。里面加个属性,如果sumit后作的动作成功了,恢复为

最初的状态,这样可能比较好。-->如果sumit再加disable=false,那它就不会回滚了,so,,,不行的.

3。现在只是有个window.confirm确认窗口阻隔了一下。

(这里可以让默认按钮为No) ,人家是想保存才按button的,所以没必要加confirm. 再加就是画蛇添足了,而且我的是可以通过control+S保存的and 一定要保存得快.

4。一般post回去会刷新页面,用户可以通过这个确认

是否已经sumit了;如用无刷新的话,用上面的windos.confim 这样比较好。-->如果post回去,那button自然回到原来的状态.

wego said:

开个异步线程显示提交进度岂不锦上添花 ?!

-->这个只是花巧的东东,并不能解决问题的.

May 14, 2008 12:28 AM
Copyright SDT, 2006-2009. All rights reserved.