Hi there,
This scenario is pretty common, but not supported by the framework directly.
Here's what I do to solve this:
1. Create a function that disables all validators on the page. You can use code like this for that:
Code:
for (var i = 0; i < Page_Validators.length; i++)
{
Page_Validators[i].enabled = false;
}
2. As parameters for this function, allow the names of the controls you *do* want to validate from one button click:
Code:
for(var j = 0; j <= arguments.length - 1; j++)
Code:
{
document.getElementById(arguments[j]).enabled = true;
}
3. Add an
onclick attribute to the button that causes validation (use:
MyButton.Attributes.Add) and have it call the function from step 1. As arguments, pass one or more
ClientIDs to this function.4. Repeat this steps for other buttons you have on your site / page. If you want, you can add the JavaScript function to its own .
js page, and link to that in your ASPX page.
Now, what happens is this: The page loads, and, let's say, two buttons have the behavior I described. Once you click button 1, you call the JavaScript function and pass '
valUserName' as the control you want to validate. The code in the function disables all validators, and then reenables the
valUserName validator. The same way, if you click button 2 and pass
valSearch, all validators are enabled, except for
valSearch.
You may need to repeat this process for the Validation Summaries (you'll find an array called
Page_ValidationSummaries at the end of your page that contains all their names). In the
onclick of the button, disable them all, except for the one that is attached to the button.
You'll also need to repeat this for browsers that don't use the client side JavaScript. However, this may be easier. In the Button_Click event of each button, you can easily figure out what the user is trying to do, then disable the validators you don't need and call
Page.Validate().
Hope this helps, and if you have any questions, feel free to ask them here. Working with the validators from client side script isn't the most intuitive thing to do.
Cheers,
Imar
---------------------------------------
Imar Spaanjaars
Everyone is unique, except for me.