asp.net中RadioButtonGroup的必需验证器

时间:2013-10-14 18:13:24

标签: c# asp.net .net sql-server

我有TextBox的验证码:

<div style="margin-top:10px" class="autenticacionTitulo">Nombre y Apellido:</div>
        <div style="margin-top:5px"><asp:TextBox ID="nombre" runat="server" MaxLength="100" ValidationGroup="Curriculum" CssClass="autenticacionTextBox"></asp:TextBox></div>
        <div class="textoError">
          <asp:RequiredFieldValidator ID="Validator2" runat="server"
          ErrorMessage="Escribe tu nombre y apellido" ControlToValidate="nombre"
          Display="Dynamic" ValidationGroup="Curriculum"></asp:RequiredFieldValidator>
        </div>

因此,如果用户没有写任何东西,它会抛出一个texterror,该字段需要填充。

现在,我需要这样的验证器,但对于RadioButtonList:

<div class="autenticacionTituloR">¿Usa Lentes?</div>
    <div style="margin-top:5px"><asp:RadioButtonList ID="RadioButtonList6" RepeatColumns = "2" RepeatDirection="Vertical" RepeatLayout="Table"  runat="server">
    <asp:ListItem ValidationGroup="Curriculum" style="margin-right:12px; margin-top:-10px" >Si</asp:ListItem>
    <asp:ListItem ValidationGroup="Curriculum" >No</asp:ListItem></asp:RadioButtonList></div>

我应该只放置相同的RequiredFieldValidator,还是还有另一个功能来完成RadioButtons?

提前致谢!

1 个答案:

答案 0 :(得分:2)

您可以使用RequiredFieldValidator来验证RadioButtonList。

ValidationGroup必须位于RadioButtonList(而不是每个ListItem)

<asp:RadioButtonList ID="RadioButtonList6" RepeatColumns="2"
    RepeatDirection="Vertical" RepeatLayout="Table" runat="server" 
    ValidationGroup="Curriculum">
    <asp:ListItem>Si</asp:ListItem>
    <asp:ListItem>No</asp:ListItem>
</asp:RadioButtonList>
<asp:RequiredFieldValidator runat="server" ID="RequiredFieldValidator1"
    ControlToValidate="RadioButtonList6" Text="Required" 
    ValidationGroup="Curriculum">
</asp:RequiredFieldValidator>