Skip to content

ValidationGroup – How to Assign Them to ValidationSummary

January 25, 2012

“When you specify a value for the ValidationSummary property, only error messages from the validation controls that are part of the specified group are summarized. If you do not set a value for this property, all error messages from validation controls on the page that are not assigned to a validation group are summarized.”

From the quotation above, this is an important face that the ValidationSummary only looks for matching ValidationGroups it is assigned to. This had been an issue I found recently with the ValidationSummary control component in that by default the control will be assigned to all validation controls on the page when you don’t assign a ValidationGroup=”…” to it.

If you do assign a ValidationGroup say ValidationGroup=”EditGroup” as shown by the code below, it still wouldn’t work  from what I found, this still needs to tie with a button which can then only work, which I found baffling but it seems to make sense as Microsoft ties validation with the buttons that issues the instructions for validation. Hence on the code to below for a password change for a user web page, I needed to write this component for the validation to occur for the password and repeat password text boxes with <asp:Button ID=”btnSaveUser” runat=”server” ValidationGroup=”EditGroup” Text=”Save User” onclick=”btnSaveUser_Click” />. ValidationGroup=”EditGroup” assigns the button to the validation controls. Plus the dynamic addition of assigning this to the required field validators. This is so, you can exclude them if you do not want to change the password for the user.

<asp:Content ContentPlaceHolderID=”content” Runat=”Server”>

<table>
<tr>
<td colspan=”3″><h2>Login Details</h2></td>
</tr>
<tr>
<td>Username</td>
<td><asp:RequiredFieldValidator id=”rfvUsername” CssClass=”errorMsg” ControlToValidate=”txtUsername” ErrorMessage=”User name is required.” Display=”Dynamic”        Width=”100%” runat=”server”>*</asp:RequiredFieldValidator></td>
<th><asp:TextBox ID=”txtUsername” runat=”server” Width=”200px”></asp:TextBox></th>
</tr>
<tr id=”trResetPasswordButton” runat=”server”>
<td></td>
<td colspan=”2″><asp:Button ID=”btnResetPassword” runat=”server” class=”button” Text=”Reset Password” onclick=”btnResetPassword_Click” /></td>
</tr>
<tr id=”trPassword” runat=”server” visible=”false”>
<td><label ID=”lblPassword” runat=”server”>Password</label></td>
<td><asp:RequiredFieldValidator id=”rfvPassword” CssClass=”errorMsg” ControlToValidate=”txtPassword” ErrorMessage=”Password is required.” Display=”Dynamic” Width=”100%” runat=”server”>*</asp:RequiredFieldValidator></td>
<th><asp:TextBox ID=”txtPassword” runat=”server” TextMode=”Password” Width=”200px”></asp:TextBox></th>
</tr>
<tr id=”trRepeatPassword” runat=”server” visible=”false”>
<td><label id=”lblRepeatPassword” runat=”server”>Repeat Password</label></td>
<td><asp:RequiredFieldValidator id=”rfvRepeatPassword” CssClass=”errorMsg” ControlToValidate=”txtRepeatPassword” ErrorMessage=”Repeat Password is required.” Display=”Dynamic” Width=”100%” runat=”server”>*</asp:RequiredFieldValidator></td>
<th><asp:TextBox ID=”txtRepeatPassword” runat=”server” TextMode=”Password” Width=”200px”></asp:TextBox><asp:CompareValidator runat=”server” id=”cmpvPasswords” controltovalidate=”txtPassword” controltocompare=”txtRepeatPassword” operator=”Equal” type=”String” errormessage=”Password and Repeat Password must match.” /></th>
</tr>
<tr>
<td>&nbsp;</td>
<td>&nbsp;</td>
<th>&nbsp;</th>
</tr>
</table>

 

protected void btnResetPassword_Click(object sender, EventArgs e)
{
if (btnResetPassword.Text == “Reset Password”)
{
trPassword.Visible = true;
trRepeatPassword.Visible = true;
rfvPassword.ValidationGroup = “EditGroup”;
rfvRepeatPassword.ValidationGroup = “EditGroup”;
btnResetPassword.Text = “Keep Current Password”;
}
else
{
trPassword.Visible = false;
trRepeatPassword.Visible = false;
rfvPassword.ValidationGroup = “”;
rfvRepeatPassword.ValidationGroup = “”;
btnResetPassword.Text = “Reset Password”;
}
}

</asp:Content>

Advertisements

From → Uncategorized

Leave a Comment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: