2014-01-19 79 views
0

我在表單中有兩個元素部分(例如比薩和漢堡)。在多個按鈕上執行驗證

每個部分有不同的字段,每個部分都有自己的提交按鈕。 現在我只想在按下相應的提交按鈕時驗證這些部分。如果按下其他提交按鈕,則不應驗證其他部分的元素。

<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

剛剛嘗試你的代碼向左轉向:) – Sachin

回答

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