ASP.NET custom button management - how to override OnClientClick but save existing behavior?

So, I have an ASP.NET 4 user element called "SafeClickButton" that is designed to override the default click behavior of the client (OnClientClick).

Essentially, I'm trying to disable a button when I click, and then perform any existing functions (validation, postback, etc.).

It looks like you are displaying HTML correctly (onclick = "this.disabled = true; __ doPostback ...), and it turns off correctly, but the problem is with page verification. If any verification on the page fails, send it back and THEN showing verification errors (where this should be done on the client side, without requiring a postback).

Here is the code for the user control.

public class SafeClickButton : Button { public override string OnClientClick { get { return string.Format("this.disabled=true;{0}", Page.ClientScript.GetPostBackEventReference(this, string.Empty)); } set { base.OnClientClick = value; } } protected override PostBackOptions GetPostBackOptions() { PostBackOptions options = new PostBackOptions(this, string.Empty) {ClientSubmit = true}; if (Page != null) { if (CausesValidation && (Page.GetValidators(ValidationGroup).Count > 0)) { options.PerformValidation = true; options.ValidationGroup = ValidationGroup; } if (!string.IsNullOrEmpty(PostBackUrl)) { options.ActionUrl = HttpUtility.UrlPathEncode(ResolveClientUrl(PostBackUrl)); } } return options; } } 

What am I doing wrong?

EDIT

Ok, so I found part of the problem:

 return string.Format("this.disabled=true;{0}", Page.ClientScript.GetPostBackEventReference(this, string.Empty)); 

Behavioral behavior of postbackoptions will not be applied.

So, I changed it to this:

 return string.Format("this.disabled=true;{0}", Page.ClientScript.GetPostBackEventReference(GetPostBackOptions())); 

Now the correct operation is checked on the client side, but the button is not enabled again, FML =)

I think I need to be smart now and say, "If the verification fails, turn on the button again."

Any ideas?

+4
source share
1 answer

You can simply add the Page_ClientValidate inline method. I have never tried this so that it does not work:

 return string.Format("if (Page_ClientValidate()) { this.disabled=true; {0} } else return false;", Page.ClientScript.GetPostBackEventReference(this, string.Empty)); 

You may need to associate with it or add some checks to support GroupValidation, but I think this will lead you to the right path.

EDIT: I updated the response and moved you to disable it if it only disconnects after Page_ClientValidate .

Check out this link as it does what you are looking for. I think and illustrate what I meant with Page_ClientValidate :

http://msmvps.com/blogs/anguslogan/archive/2004/12/22/27223.aspx

+2
source

All Articles