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?