asp:RadioButtonList asp:RequiredFieldValidator永远不会激活验证

时间:2017-03-16 17:36:02

标签: asp.net validation

我有一个webforms页面(不要问)我正在尝试使用标准验证器进行一些客户端验证。

下面是一些部分代码

<asp:TemplateField ItemStyle-Width="50px" HeaderText="Received">
        <HeaderStyle  HorizontalAlign="Center" />
               <ItemTemplate>
                    <asp:RadioButtonList ID="rbIsRecv"  RepeatDirection="Horizontal" 
                      runat="server" ValidationGroup="Curriculum">
                       <asp:ListItem Value="1">Yes&nbsp;</asp:ListItem>
                        <asp:ListItem Value="0">No</asp:ListItem>
                     </asp:RadioButtonList>
                    <asp:RequiredFieldValidator runat="server" ID="RequiredFieldValidator1" 
                   ControlToValidate="rbIsRecv" Text="Required" 
                    ValidationGroup="Curriculum" />
          </ItemTemplate>
            </asp:TemplateField>      

&LT; .......&GT;

<asp:TemplateField ItemStyle-Width="50px" HeaderText="Actually Received">
                                <HeaderStyle  HorizontalAlign="Center" />
                                <ItemTemplate>
                                    <asp:TextBox ID="txtActual" runat="server" />  
                                    <asp:RequiredFieldValidator ForeColor="Red" ValidationGroup="AllValidators" runat="server" 
                                        ControlToValidate="txtActual" ErrorMessage="All values must be filled in!" />                                 
                                </ItemTemplate>
                            </asp:TemplateField>
                        </Columns>
                    </asp:GridView>
                    </div>
                </div>          
            </div>
             <div class="row pull-right">
                <asp:Label runat="server">Employee ID: </asp:Label> 
                <asp:TextBox ID="txtName" runat="server" /><br />
                <asp:Button ID="btnSubmit" runat="server" Text="Submit" OnClick="btnSubmit_Click" CssClass="btn-primary pull-right" />
            </div>  
            <div class="row">
                <asp:RequiredFieldValidator ForeColor="Red" ValidationGroup="AllValidators" runat="server" ControlToValidate="txtName" ErrorMessage="Please enter your employee Number!" />     
                <asp:ValidationSummary ID="ValidationSummary1" runat="server" ValidationGroup="AllValidators"/>
            </div>

因此,当我正确地从txtName控件向Summary Control提交它时抛出错误,但网格中没有任何控件....

有什么想法吗?

1 个答案:

答案 0 :(得分:1)

您无法使用RadioButtonList验证RequiredFieldValidator。你需要CustomValidator

<asp:CustomValidator ID="CustomValidator1" runat="server" ErrorMessage="Select an option"
    ClientValidationFunction="validateRadioButtonList"></asp:CustomValidator>

<script type="text/javascript">
    function validateRadioButtonList(oSrc, args) {
        if ($("input[name='<%= RadioButtonList1.UniqueID %>']:checked").val() == null) {
            args.IsValid = false;
        } else {
            args.IsValid = true;
        }
    }
</script>