对多个按钮执行验证

时间:2014-01-19 19:34:43

标签: asp.net

我在表单中有两部分元素(例如披萨和汉堡)。

每个部分都有不同的字段,每个部分都有自己的提交按钮。 现在,我只想在按下相应的提交按钮时验证这些部分。如果按下另一个提交按钮,则不应验证其他元素部分。

<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>

1 个答案:

答案 0 :(得分:2)

您需要使用MSDN -

中的验证组
  

验证组允许您在页面上组织验证控件   作为一组。每个验证组都可以独立执行验证   来自页面上的其他验证组。

例如(来自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" />