Call javascript function when Page_isValid changes?
On my asp.net form,开发者_Go百科 I'd like to disable the submit button whenever its validators fail—i.e., anytime Page_isValid = false
—and re-enable it afterwards.
I can do this by responding to the onchange
event of each control and testing for Page_isValid
, but I'd much rather respond to the changing of the variable itself, or to the execution of the function that does the changing.
From my research so far, here's what I've concocted:
Sys.Application.add_load(AddValidatorHandlers);
function AddValidatorHandlers() {
var validatorList;
try { validatorList = Page_Validators; } catch (err) { }
if (validatorList) {
for (i in validatorList) {
validatorList[i].onpropertychange = function () { "MY CODE GOES HERE" };
}
}
}
It works, but not very efficiently. I have to loop through all the validators, and my function always fires multiple times in a row.
Is there a way I can simply (a) respond to the alteration of that Page_isValid
variable? or else (b) get a list of validation groups rather than the individual validators?
Thanks!
For these types of situations, I always like to put my validators next to the actual code that alters what I'm checking for.
In your case, every time you alter your Page_isValid variable, run some logic that checks if it changed values compared to what it was previously. If it did, check if it is now True or False, and disable or enable your appropriate page elements.
If you need to access multiple page elements, you can create an "updatePageElementsViaValidator" function and call it with true or false as a parameter using the same logic; this way, you can enable or disable entire chunks of your page with a single function call.
This logic can be modified to suit your personal preferences, but the main point is that it's much easier to track changes to variable values at the instance you change them as opposed to when you need to access them.
精彩评论