下拉列表中的RequiredFieldValidator不起作用

时间:2014-07-07 18:57:03

标签: asp.net requiredfieldvalidator

我做错了什么?

   <asp:Panel ID="pnlProductUnits" runat="server" Width="100%">


         <asp:Repeater ID="repProductUnits" runat="server" EnableViewState="true" onitemcommand="Button_ItemCommand">
    <HeaderTemplate>
    <table class="grid" width="100%">
    <tbody>
    <tr>
        <th align="left">test1</th>
        <th align="left">test2</th>
        <th align="left">test3</th>
        <th width="50"></th>
    </tr>
    </tbody>

    </HeaderTemplate>
    <ItemTemplate>
    <tr>
    <td><%# ((DBData.Catalog.ProductU)Container.DataItem).PrimaryUnitName%>
    </td>
    <td align="center">
    <%# ((DBData.Catalog.ProductU)Container.DataItem).SecondaryUnitName%>
    </td>
    <td align="center">
    <%# ((DBData.Catalog.ProductU)Container.DataItem).Quantity%>
    </td>
    <td style="display:none">
    <asp:Label ID="PrimaryUnitID" runat="server" 
     Text="<%# ((DBData.Catalog.ProductU)Container.DataItem).PrimaryUnitID%>">
   </asp:Label>
   <asp:Label ID="SecondaryUnitID" runat="server" 
    Text="<%# ((DBData.Catalog.ProductU)Container.DataItem).SecondaryUnitID%>">
   </asp:Label>
</td>
<td align="center">

  <asp:Button id="btRemove"  runat="server" commandname="deleteProductUnit" Text="Delete"> 
  </asp:Button>
  </td>
</tr>
</ItemTemplate>
<FooterTemplate>
<tr>
   <td>
   <asp:DropDownList ID="ddlPrimaryUnit" runat="server" ValidationGroup="grpQuantity">
    </asp:DropDownList>
  <asp:RequiredFieldValidator runat="server" ErrorMessage="*" 
   Display="Dynamic" Text="*" ControlToValidate="ddlPrimaryUnit" ValidationGroup="grpQuantity" ForeColor="#FF3300"> 
  </asp:RequiredFieldValidator>
 </td>
<td>
    <asp:DropDownList ID="ddlSecondaryUnit" runat="server" ValidationGroup="grpQuantity">
            </asp:DropDownList>
<asp:RequiredFieldValidator runat="server" ErrorMessage="*" Display="Dynamic" Text="*" ControlToValidate="ddlSecondaryUnit" ValidationGroup="grpQuantity" ForeColor="#FF3300"></asp:RequiredFieldValidator>
</td>
  <td>
   <asp:TextBox ID="txtQuantity" runat="server" placeholder="select value" ClientIDMode="Static"></asp:TextBox>
<asp:RequiredFieldValidator runat="server" ErrorMessage="*"  ControlToValidate="txtQuantity" ValidationGroup="grpQuantity" ForeColor="#FF3300"></asp:RequiredFieldValidator>
</td>
       <td colspan="20">
   <asp:Button id="btNew"  runat="server" commandname="addProductUnit" Text="Add" 
    CommandArgument="test" ValidationGroup="grpQuantity"> </asp:Button>
  </td>
</tr>
</table>
</FooterTemplate>
</asp:Repeater>
</asp:Panel>

1 个答案:

答案 0 :(得分:0)

我怀疑您的问题与验证者有关,知道DropDownList的原始选择是什么以及是否已选择某些内容。您可能需要将InitialValue添加到必填字段验证程序。

<asp:RequiredFieldValidator ID="RequiredFieldValidator1"
     runat="server" ErrorMessage="*" Display="Dynamic" Text="*" 
     ControlToValidate="ddlSecondaryUnit" ValidationGroup="grpQuantity" 
     ForeColor="#FF3300" InitialValue=""></asp:RequiredFieldValidator>

根据您的DropDownList的数据源,该值可能类似于&#34;请选择&#34;或者只是空白。