asp:RequiredFieldValidator和radiobuttons

时间:2012-10-06 23:56:29

标签: asp.net validation

这是我在stackoverflow.com上的第一个问题。

我正在开展一个学校项目,我必须验证用户的输入。每次页面加载时,都会给出服务器错误消息。请查看代码和之后的错误消息。

<div>
<table>
<td>
<asp:RadioButton ID="RadioButton1" runat="server"></asp:RadioButton>
<asp:RequiredFieldValidator ID="validateCheck" runat="server"  ControlToValidate="RadioButton1"   ErrorMessage="Please Enter" Display="Dynamic"></asp:RequiredFieldValidator>             
</td>
</table>
</div>

Server Error in '/' Application.
Control 'RadioButton1' referenced by the ControlToValidate property of 'validateCheck' cannot be validated.

2 个答案:

答案 0 :(得分:6)

RequiredFieldValidator不验证RadioButton。但是,您可以使用RadioButtonList控件(由RequiredFieldValidator验证)。

答案 1 :(得分:3)

asp:RadioButton不支持验证,而不是RadioButton使用RadioButtonList:'

    <form id="form1" runat="server">
<div>

    <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" 
        ErrorMessage="RequiredFieldValidator" ControlToValidate="RadioButtonList1"></asp:RequiredFieldValidator>

</div>
<asp:ValidationSummary ID="ValidationSummary1" runat="server" />

<asp:RadioButtonList ID="RadioButtonList1" runat="server">
    <asp:ListItem>One</asp:ListItem>
    <asp:ListItem>Two</asp:ListItem>
</asp:RadioButtonList>

</form>