Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/37.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/vb.net/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/jquery-ui/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net 如何检查至少一个RadioButtonList是否选中了一个项目?_Asp.net_Vb.net_Validation_Radiobuttonlist - Fatal编程技术网

Asp.net 如何检查至少一个RadioButtonList是否选中了一个项目?

Asp.net 如何检查至少一个RadioButtonList是否选中了一个项目?,asp.net,vb.net,validation,radiobuttonlist,Asp.net,Vb.net,Validation,Radiobuttonlist,我在一页上有20个无线电按钮列表 我需要创建一个验证方法,以确保至少有一个RadioButtonLists选择了一个项目 对此,我需要使用哪种验证?您可以为RadioButtonList设置默认值,这意味着用户永远无法选择选项,您也不需要所有验证代码 <asp:RadioButtonList ID="RadioButtonList1" runat="server"> <asp:ListItem Selected="True">Never</asp:ListI

我在一页上有20个无线电按钮列表

我需要创建一个验证方法,以确保至少有一个
RadioButtonList
s选择了一个项目


对此,我需要使用哪种验证?

您可以为RadioButtonList设置默认值,这意味着用户永远无法选择选项,您也不需要所有验证代码

<asp:RadioButtonList ID="RadioButtonList1" runat="server">
    <asp:ListItem Selected="True">Never</asp:ListItem>
    <asp:ListItem>Twice A Week</asp:ListItem>
    <asp:ListItem>Every Day Baby!</asp:ListItem>
</asp:RadioButtonList>

从未
一周两次
每天,宝贝!
编辑
正如下面的评论中所指出的,这本身不足以作为一种验证手段。在服务器端验证所有用户输入也是一种很好的做法

编辑:根据评论和澄清更新问题

如果要根据多个
RadioButtonList
s进行验证,则需要使用
CustomValidator
并执行服务器端检查

以下是一些测试标记:

<asp:RadioButtonList ID="rblstTest1" runat="server" ValidationGroup="Test">
    <asp:ListItem Text="Test 1" Value="1" />
    <asp:ListItem Text="Test 2" Value="2" />
    <asp:ListItem Text="Test 3" Value="3" />
</asp:RadioButtonList>
<br /><br />
<asp:RadioButtonList ID="rblstTest2" runat="server" ValidationGroup="Test">
    <asp:ListItem Text="Test 1" Value="1" />
    <asp:ListItem Text="Test 2" Value="2" />
    <asp:ListItem Text="Test 3" Value="3" />
</asp:RadioButtonList><br />
<asp:Button ID="btnTestRb" runat="server" ValidationGroup="Test" Text="Test RBL" 
    OnClick="btnTestRb_Click" />
<asp:CustomValidator runat="server" ValidationGroup="Test" ID="cvTest" 
    ControlToValidate="rblstTest1" OnServerValidate="cvTest_ServerValidate" 
    ValidateEmptyText="true" Enabled="true" display="Dynamic" SetFocusOnError="true"
    ErrorMessage="You must select at least one item." /> 
然后执行服务器端
CustomValidator
检查:

protected void cvTest_ServerValidate(object sender, ServerValidateEventArgs e)
{            
    int count = this.GetAllControls().OfType<RadioButtonList>().
        Where(lst => lst.SelectedItem != null).Count();
    e.IsValid = (count > 0);
 }
protectedvoid cvTest\u ServerValidate(对象发送方,ServerValidateEventArgs e)
{            
int count=this.GetAllControls().OfType()。
其中(lst=>lst.SelectedItem!=null).Count();
e、 IsValid=(计数>0);
}

我已经测试了上面的示例,它似乎正是您所需要的。您应该能够很容易地将其切换到VB。希望这能解决您的问题。

我使用了一种适用于ListControl的扩展方法

        public static bool IsAnyItemSelected(this ListControl input) { return input.Items.Cast<ListItem>().Aggregate(false, (current, listItem) => current | listItem.Selected); }
public static bool IsAnyItemSelected(此ListControl输入){return input.Items.Cast().Aggregate(false,(current,listItem)=>current | listItem.Selected);}

这仍然允许在不使用单选按钮选项的情况下创建http请求。需要配套代码服务器端来执行它。只是澄清一下,我有20个不同的RadioButtonList。没有必填字段,但我需要至少填写其中一个,然后才能提交表单。
        public static bool IsAnyItemSelected(this ListControl input) { return input.Items.Cast<ListItem>().Aggregate(false, (current, listItem) => current | listItem.Selected); }