Skip to content

Instantly share code, notes, and snippets.

@pjmagee
Created February 10, 2014 15:57
Show Gist options
  • Select an option

  • Save pjmagee/8918394 to your computer and use it in GitHub Desktop.

Select an option

Save pjmagee/8918394 to your computer and use it in GitHub Desktop.
// You are doomed if you use anchors.
// Page_IsValid is always true as its validators are never triggered. This only works on a form submit.
// I have come up with a solution that works well using anchors and using the client side asp.net validation functions with web forms:
// Since the anchors can contain many general things, there a few anchors you would want to exclude, including those which open modals or add / remove rows when posting data.
// We know any btn-cancel could potentially close a modal, we ignore these
// we know any void(0) functions are going to open up a modal, or they will add/remove rows, we ignore these
// We know exports of results could be downloaded multiple times, we ignore these
var excludes = $('a[title*="Export"], a.btn-cancel, a[href="javascript:void(0)"]');
// Include any anchors that could will endure a post back (__DoPostBack, PostBackWithOptions)
var includes = $('a.button[href*="PostBack"], a.button');
$(includes).not(excludes).click(function (e)
{
// the page is valid after controls validate the inputs in the form
if (Page_ClientValidate())
{
// disable the anchor, give it a disabled class, be careful removing the href or javascript if the function has not already been called.
$(this)
.attr('disabled', 'disabled')
.attr('title', 'Please wait...')
.click(function (event) { event.preventDefault(); }); // stop any further click events from triggering again
// optional features and things to do
var icon = $(this).find('i'); // font awesome icon to replace any current icon with a loading icon
// Acknowledge that the page is loading / in progress.
if (icon)
{
$(icon).removeClass().addClass('fa fa-spinner fa-spin');
}
}
else
{
e.preventDefault(); // Page not valid, do not trigger post back.
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment