Customvalidator:检查radiobuttonlist是否包含所选项目

时间:2010-05-17 20:10:01

标签: asp.net .net vb.net radiobuttonlist customvalidator

我有一个包含两个项目的radiobuttonlist,是或否.radicalobuttonlist控件有一个customvalidator,需要servervalidation函数和javascript clientvalidation函数。你可以帮帮我吗?此消息中的函数有效,但只有当我实际选择了两个列表项中的一个时,如果没有选择列表项,则验证会跳过我的radiobuttonlist控件。

function ValidateRadioButtonList(source, arguments) {
        var RBL = document.getElementById(source.controltovalidate);
        var radiobuttonlist = RBL.getElementsByTagName("input");
        var counter = 0;
        var atLeast = 1
        for (var i = 0; i < radiobuttonlist.length; i++) {
            if (radiobuttonlist[i].checked) {
                counter++;
            }
        }
        if (atLeast = counter) {
            arguments.IsValid = true;
            return arguments.IsValid;
        }
        arguments.IsValid = false;
        return arguments.IsValid;
    }

编辑:评论中的相关代码

<asp:Button ID="btnSubmit" runat="server" Text="Submit" CssClass="btnNormal"
      CausesValidation="True" />
<asp:CustomValidator runat="server"
      ClientValidationFunction="ValidateRadioButtonList"
      OnServerValidate="RadioButtonListServerValidation" ID="cvRadioButtonList"
      Font-Bold="True" Font-Size="Medium" ErrorMessage="Business critical"
      ControlToValidate="rblBusinessCritical">*</asp:CustomValidator>
<asp:RadioButtonList ID="rblBusinessCritical" runat="server" RepeatLayout="Flow"
      RepeatDirection="Horizontal" TabIndex="4">
    <asp:ListItem Text="Yes" Value="1" />
    <asp:ListItem Text="No" Value="0" />
</asp:RadioButtonList>

代码背后:

Public Sub RadioButtonListServerValidation(ByVal sender As Object, _ 
            ByVal args As ServerValidateEventArgs)
    If rblBusinessCritical.SelectedValue = "-1" Then
        args.IsValid = False
        cvRadioButtonList.ErrorMessage = "Business critical needed"
        Exit Sub
    Else
        args.IsValid = True
    End If
End Sub

4 个答案:

答案 0 :(得分:2)

您是否将CustomValidator的ValidateEmptyText Property设置为true?

编辑: 您是否将Submit-Button / RadioButtonList的CausesValidation属性设置为true? 请提供您的aspx页面的一些代码。

答案 1 :(得分:0)

这是我尝试过的另一个javascript clientvalidation函数:

function ValidateRadioButtonList(source, arguments) {
    var RBL = document.getElementById(source.controltovalidate);
    var radio = RBL.getElementsByTagName("input");
    var isChecked = false;
    for (var i = 0; i < radio.length; i++) {
        if (radio[i].checked) {
            isChecked = true;
            break;
        }
    }
    if (!isChecked) {
        alert("Please select an item");
        arguments.IsValid = false;
    }
    arguments.IsValid = true;
}

答案 2 :(得分:0)

您需要使用客户端吗?

这是服务器端解决方案......

<asp:RadioButtonList id="radTerms" runat="server">
  <asp:listitem id="optDisagree" runat="server"  value="Disagree" selected="true">I don't agree</asp:ListItem>
  <asp:listitem id="optAgree" runat="server" value="Agree">I agree</asp:ListItem>
</asp:RadioButtonList>

<asp:CustomValidator Display="Dynamic" ErrorMessage="You have to agree to the terms and conditions" ID="cmpTerms" ControlToValidate="radTerms" SetFocusOnError="true" runat="server" OnServerValidate="cmpTermsAccepted_ServerValidate">*</asp:CustomValidator>

代码隐藏:

protected void cmpTermsAccepted_ServerValidate(Object source, System.Web.UI.WebControls.ServerValidateEventArgs args)
{
    args.IsValid = (args.Value == "Agree");
}

答案 3 :(得分:0)

<asp:RadioButtonList ID="LocationAccurateRBL" CssClass="radioButtonList" RepeatDirection="Horizontal" RepeatColumns="4" RepeatLayout="Flow" runat="server">
                            <asp:ListItem  Text="Yes" Value="1" />
                            <asp:ListItem Text="No" Value="0" />
                        </asp:RadioButtonList>

 <asp:CustomValidator runat="server" ID="CheckBoxRequired" EnableClientScript="true" ControlToValidate="LocationAccurateRBL"
                            ClientValidationFunction="LocationAccurate_ClientValidate" ValidateEmptyText="true"
                            Text="*" ForeColor="Red" ErrorMessage="Please let us know if the location is accurate" SetFocusOnError="true" ValidationGroup="CreateVG" />

脚本由于jquery而短得多。这将做你想要的。

  <script>
                        function LocationAccurate_ClientValidate(sender, e) {
                            e.IsValid = $("#<%=LocationAccurateRBL.ClientID%> > input").is(':checked');
                        }                          
                    </script>