Here's a handy tip on setting the validation group property for a ASP.NET ValidationSummary Control in JavaScript.
Scenario
We have a large form with three input areas (not my design!). Each input area has its own Save/Cancel button set and its own ValidationGroup. As well, we have a single ValidationSummary control on the master page to display errors raised by the validation controls.
Based upon which Save button user presses, we want to be able to dynamically set the validationGroup on the Validation Summary control so that it picks up the error messages from the validators in the selected validation group – all client-side.
If you look at the client-side representation of the ValidationSummary control (Page_ValidationSummaries[0]) in a quick watch, you are discouraged when you do not see a property for ValidationGroup, leading you to believe that this value is not exposed as a client-side attribute of the ValidationSummary control:
However, fear not, when you get into an immediate window, you can ‘manually’ append ".validationGroup" to the ValidationSummary object….. and, it works!
And, sure enough in your JavaScript function, you too can set your validation group on your ValidationSummary control:
//-- Dynamically set validation group on a validation summary control.
function SetValidationGroup(validationGroupName) {
//debugger;
//-- Check for null validation group name
if (validationGroupName == null) {
return;
}
//-- Assign validation group name to validationSummary control
Page_ValidationSummaries[0].validationGroup = validationGroupName;
}
Then, in you button control, add the 'onClientClick="SetValidationGroup(validationGroupName);"
<
asp:Button ID="myButton" runat="server" Text=" Save " OnClick="myButtonServerSideEventHandler" ValidationGroup="ValidationGroupForFirstButton" OnClientClick="SetValidationGroup(ValidationGroupForFirstButton);" />
Keep in mind that this example 'assumes' that you have a single ValidationSummary control (Element 1 of the zero-based Page_ValidationSummaries collection). You know what they say about, "assume." Add some code to ensure that you are setting the correct ValidationSummary control.
Hope this helps.
Looking forward to seeing Dallas-area .NET developers at the DDNUG meeting on April 8th at Microsoft.
Be prepared to go deep into the new debugging features in to VS 2010.
Especially excited about IntelliTrace (formerly, Historical Debugging), which allows you to move back in time within your debugging session, without having to restart the application.
Here are some great links to help you get started with IntelliTrace:
In meantime, drop me a line if you need anything ==> robvettor@hotmail.com
Looking forward to seeing OK City .NET developers at the OK City .NET UG on Monday Feb. 1st.
Be prepared to go deep into the new debugging features in to VS 2010.
Especially excited about IntelliTrace (formerly, Historical Debugging), which allows you to move back in time within your debugging session, without having to restart the application.
Here are some great links to help you get started with IntelliTrace:
In meantime, drop me a line if you need anything ==> robvettor@hotmail.com
Very much enjoyed presenting "What's Cool in VS 2010" to HDNUG last week in Houston.
Man, what a LIVELY BUNCH -- you guys seriously ROCKED!
Hope you saw the power in the upcoming version of VS 2010.
Special thanks to Michael Steinburg for coordinating and the leadership of that team, including J Swayer and Zain Naboulsi.
Hope to see you later this year at Houston Tech Fest, and, in meantime, drop me a line if you need anything ==> robvettor@hotmail.com
ps ==> SlideDeck is attached!