Introduction
I found a big problem in ASP.NET when I used multiple validation groups in a page.
The Button click event does not check every validation group.
Using the code
I assumed that you have controls with multiple validation group, like this:
<asp:DropDownList ID="ddlIndBrand" runat="server" CssClass="contentControlWidth2"
DataTextField="BrandName" DataValueField="BrandID" ValidationGroup="DBudgetEntry"
Width="150px">
</asp:DropDownList>
<asp:RequiredFieldValidator ID="RequiredFieldValidator15"
runat="server" ControlToValidate="ddlIndBrand"
Display="None" ErrorMessage="Please select a Brand!"
ForeColor="Red" InitialValue="0"
SetFocusOnError="True" Text="?"
ValidationGroup="group1"></asp:RequiredFieldValidator>
<asp:TextBox ID="txt" runat="server" Columns="5"
CssClass="contentControlWidth2"
Height="70px" TextMode="MultiLine"
ValidationGroup="DBudgetEntry" Width="215px"></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator16"
runat="server" ControlToValidate="txt"
Display="None" ErrorMessage="Details required!"
ForeColor="Red" SetFocusOnError="True"
Text="?" ValidationGroup="group2"></asp:RequiredFieldValidator>
<asp:TextBox ID="txt2" runat="server"
Columns="5" CssClass="contentControlWidth2"
Height="70px" TextMode="MultiLine"
ValidationGroup="DBudgetEntry" Width="215px"></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator16"
runat="server" ControlToValidate="txt2"
Display="None" ErrorMessage="Details required!"
ForeColor="Red" SetFocusOnError="True"
Text="?" ValidationGroup="group3"></asp:RequiredFieldValidator>
<asp:Button ID="btnSave" runat="server"
Text="Save" Visible="True" Width="60px"
OnClientClick="javascript:return validatePage();"/>
Write this on the top of the .aspx page:
<script language="javascript" type="text/javascript">
function validatePage() {
var flag = window.Page_ClientValidate('group1');
if (flag)
flag = window.Page_ClientValidate('group2');
if (flag)
flag = window.Page_ClientValidate('group3');
if (flag)
flag = window.Page_ClientValidate();
return flag;
}
</script>
This validates the client side. But for the server side, write this on the btnsave
click event:
protected void btnSave_Click(object sender, EventArgs e)
{
Page.Validate();
if (!Page.IsValid)
{
return;
}
}