Reputation: 21
I have two section of elements inside the form (e.g. pizza and burger).
Each section has different fields and each section has its own submit button. Now I want to validate each of these section only if their corresponding submit button is pressed. If the other submit button is pressed, it should not validate other section of elements.
<asp:UpdatePanel ID="UpdatePanel2" runat="server">
<ContentTemplate>
<asp:Panel ID="Panel2" runat="server" GroupingText="Burger" BackImageUrl="~/finalll/check 24/check 24/images/background-burger.jpg"
Width="684px">
<asp:Label ID="burgertype" runat="server" Text="Burger Type" Width="249px"></asp:Label><br />
<asp:DropDownList ID="DropDownListburger" AppendDataBoundItems="true" runat="server"
OnLoad="DropDownListburger_Load">
<asp:ListItem Text="<Select One>" Value="" />
</asp:DropDownList>
<asp:RequiredFieldValidator ID="RequiredFieldValidatorbtype" runat="server" InitialValue=""
ControlToValidate="DropDownListburger" ForeColor="Red" ErrorMessage="*"></asp:RequiredFieldValidator>
<br />
<asp:Label ID="burgersize" runat="server" Text="Burger Size" Width="249px"></asp:Label><br />
<asp:DropDownList ID="DropDownbsize" runat="server">
<asp:ListItem Text="Select One" Value=""></asp:ListItem>
<asp:ListItem Text="small" Value="small"></asp:ListItem>
<asp:ListItem Text="medium" Value="medium"></asp:ListItem>
<asp:ListItem Text="large" Value="large"></asp:ListItem>
<asp:ListItem Text="Extra Large" Value="Extra Large"></asp:ListItem>
</asp:DropDownList>
<asp:RequiredFieldValidator ID="RequiredFieldValidatorbsize" runat="server" InitialValue=""
ControlToValidate="DropDownbsize" ForeColor="Red" ErrorMessage="*"></asp:RequiredFieldValidator>
<br />
<asp:Label ID="Labelquantity2" runat="server" Text="Quantity" Width="249px"></asp:Label><br />
<asp:TextBox ID="TextBoxquantity2" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidatorquanty2" runat="server" Text="*"
ControlToValidate="TextBoxquantity2" ForeColor="Red"></asp:RequiredFieldValidator>
<asp:RegularExpressionValidator ID="RegularExpressionValidatorquanty2" runat="server"
ErrorMessage="Enter Valid Quantity" ControlToValidate="TextBoxquantity2" CssClass="requiredFieldValidateStyle"
ForeColor="Red" ValidationExpression="[0-9]+">
</asp:RegularExpressionValidator>
<br />
<asp:Button ID="Buttonburger" runat="server" Text="submit" OnClick="Buttonburger_Click" />
</asp:Panel>
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="Buttonburger" EventName="Click" />
</Triggers>
</asp:UpdatePanel>
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<asp:Panel ID="Panel1" runat="server" GroupingText="Pizza" BackImageUrl="~/finalll/check 24/check 24/images/1380310811-pitstsa-ch1--39.jpg"
Width="684px">
<asp:Label ID="Pizzatype" Text="Pizza Type" runat="server"></asp:Label>
<br />
<asp:DropDownList ID="dropdwnpizza" AppendDataBoundItems="true" runat="server" OnLoad="dropdwnpizza_Load">
<asp:ListItem Text="<Select One>" Value="" />
</asp:DropDownList>
<asp:RequiredFieldValidator ID="rfqdropdwnpizza" runat="server" InitialValue="" ControlToValidate="dropdwnpizza"
ForeColor="Red" ErrorMessage="*"></asp:RequiredFieldValidator>
<br />
<asp:Label ID="pizzasize" Text="Pizza Size" runat="server"></asp:Label><br />
<asp:DropDownList ID="DropDwnsize" runat="server">
<asp:ListItem Text="Select One" Selected="True" Value=""></asp:ListItem>
<asp:ListItem Text="small" Value="small"></asp:ListItem>
<asp:ListItem Text="medium" Value="medium"></asp:ListItem>
<asp:ListItem Text="large" Value="large"></asp:ListItem>
<asp:ListItem Text="Extra Large" Value="Extra Large"></asp:ListItem>
</asp:DropDownList>
<asp:RequiredFieldValidator ID="rfqpizzasize" runat="server" InitialValue="" ControlToValidate="DropDwnsize"
ForeColor="Red" ErrorMessage="*"></asp:RequiredFieldValidator>
<br />
<asp:Label ID="Labelquantity" runat="server" Text="Quantity" Width="249px"></asp:Label><br />
<asp:TextBox ID="TextBoxquantity" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator ID="pprc" runat="server" Text="*" ControlToValidate="TextBoxquantity"
ForeColor="Red"></asp:RequiredFieldValidator>
<asp:RegularExpressionValidator ID="RegularExpressionValidatorpprc" runat="server"
ErrorMessage="Enter Valid Quantity" ControlToValidate="TextBoxquantity" CssClass="requiredFieldValidateStyle"
ForeColor="Red" ValidationExpression="[0-9]+">
</asp:RegularExpressionValidator>
<asp:Button ID="Buttonpizza" runat="server" Text="submit" OnClick="Buttonpizza_Click" />
</asp:Panel>
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="Buttonpizza" EventName="Click" />
</Triggers>
</asp:UpdatePanel>
Upvotes: 0
Views: 163
Reputation: 3012
You need to use Validation Groups, from MSDN -
Validation groups allow you to organize validation controls on a page as a set. Each validation group can perform validation independently from other validation groups on the page.
For example (from MSDN):
<asp:requiredfieldvalidator id="RequiredFieldValidator2"
controltovalidate="AgeTextBox"
validationgroup="PersonalInfoGroup"
errormessage="Enter your age."
runat="Server">
</asp:requiredfieldvalidator>
<!--When Button1 is clicked, only validation controls that are a part of PersonalInfoGroup
are validated.-->
<asp:button id="Button1"
text="Validate"
causesvalidation="true"
validationgroup="PersonalInfoGroup"
runat="Server" />
Upvotes: 2