RepeatHost Business Hosting

Reply to comment

Now working with ValidationGroups

Right, I've now got this working with ValidationGroups by passing the group name to the Page_ClientValidate() method like this: Page_ClientValidate('validationgroup')

If like me you are registering the client script server side using ClientScript.RegisterClientScriptBlock(), you will need to register a separate script for each validation group. This caught me out at first as I use a helper routine, calling it for each button that I wanted the "click once" functionality applied to. This helper registers the client script, but because the "key" parameter was the same each time, only the first one got registered (with whatever ValidationGroup happened to be on that button). I got around this by including the ValidationGroup name in the RegisterClientScriptBlock() "key" parameter, ensuring that a separate client script is registered for each validation group. My complete helper routine is as follows:-

public static void ForceSingleButtonClick(Page page, Button button)
{
const string StyleName = "disabled_button";

string scriptKey = "clickoncebtn" + button.ValidationGroup;

if (!page.ClientScript.IsClientScriptBlockRegistered(scriptKey))
{
// Register script (use both IE and FF class attribute names).
StringBuilder sb = new StringBuilder();
sb.Append(" function disableButtonOnClick(oButton)");
sb.Append(" {");
sb.Append(" if (typeof(Page_ClientValidate) == 'function') ");
sb.AppendFormat(" if (Page_ClientValidate('{0}') == false) return false; ", button.ValidationGroup);
sb.Append(" oButton.disabled = true; ");
sb.AppendFormat(" oButton.setAttribute('className', '{0}'); ", StyleName);
sb.AppendFormat(" oButton.setAttribute('class', '{0}');", StyleName);
sb.Append("}");

// Register a separate script for each validation group.
page.ClientScript.RegisterClientScriptBlock(
typeof(WebUtility),
scriptKey,
sb.ToString(),
true);
}

// Alter button click to call javascript before normal postback.
button.UseSubmitBehavior = false;

PostBackOptions options = new PostBackOptions(button);

button.OnClientClick =
"disableButtonOnClick(this); " +
page.ClientScript.GetPostBackEventReference(options) + "; return;";
}

Hope this helps someone! I would be interested to hear of any problems etc.
Andy (andrew.stephens@cheshire.gov.uk)

Reply