Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/340.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
C# 如何检查RadioButton列表是否选中_C#_Asp.net_Radiobuttonlist_Selectedvalue_Selectedindexchanged - Fatal编程技术网

C# 如何检查RadioButton列表是否选中

C# 如何检查RadioButton列表是否选中,c#,asp.net,radiobuttonlist,selectedvalue,selectedindexchanged,C#,Asp.net,Radiobuttonlist,Selectedvalue,Selectedindexchanged,我不熟悉asp.net和c。正在尝试检查是否选中单选按钮。如果选中,则else子句将起作用,如果未选中,则希望将lbl_hata设置为“EMPTY!!”文本 我还尝试了selectedIndexChanged,但它不起作用。 我怎样才能解决这个问题 if (RadioButtonList_gorusmeYapilanOkul.SelectedValue == "") { lbl_hata.Text = "EMPTY!!"; } if (yetkiliAdSoyad_txt.Text=="

我不熟悉asp.net和c。正在尝试检查是否选中单选按钮。如果选中,则else子句将起作用,如果未选中,则希望将lbl_hata设置为“EMPTY!!”文本

我还尝试了selectedIndexChanged,但它不起作用。 我怎样才能解决这个问题

if (RadioButtonList_gorusmeYapilanOkul.SelectedValue == "")
{
    lbl_hata.Text = "EMPTY!!";
}
if (yetkiliAdSoyad_txt.Text=="")
{
    lbl_hata.Text = "EMPTY!!";
}
这是aspx页面

<td class="auto-style2">
    <asp:RadioButtonList ID="RadioButtonList_gorusmeYapilanOkul" runat="server" Width="174px" >
        <asp:ListItem Value="Seyrantepe Şube 1">Seyrantepe Şube 1</asp:ListItem>
        <asp:ListItem Value="Seyrantepe Şube 2">Seyrantepe Şube 2</asp:ListItem>
    </asp:RadioButtonList>
</td>

塞兰特佩河1号
塞兰特佩河2号

selectedIndexChanged是当用户选择组中的单选按钮时将触发(调用)的事件

这就像你正在尝试检查RadioButtonList_gorusmeYapilanOkul是否有一个选中的选项。我假设这是一个不同的事件(比如用户点击了一个按钮)。如果是这种情况,则不需要捕获selectedIndexChanged事件

我建议您使用以下代码:

if (RadioButtonList_gorusmeYapilanOkul.SelectedValue == "")
{
    lbl_hata.Text = "EMPTY!!";
}
else
{
    lbl_hata.Text = "Not empty!!";
}
然后在第一行上放置一个断点并使用调试器进行遍历,这将允许您了解发生了什么以及逻辑是如何流动的

更新: 我不想将此作为评论发布,因为它会丢失格式。 您可以尝试以下代码:

if (RadioButtonList_gorusmeYapilanOkul.SelectedIndex > -1) 
{  
    lbl_hata.Text = "You selected: " + RadioButtonList_gorusmeYapilanOkul.SelectedItem.Text;
}
else
{
    lbl_hata.Text = "You didn't select anything";
}

您在
lbl_hata.Text=“EMPTY!”之后缺少一个
,可能这不是因为你编辑了你的帖子而导致的错误?我错过了“写在这里。但它在我的代码中有。并且不起作用。请解释:不起作用。我的else子句在mysql中插入数据。当我在运行项目后查看我的数据库时,如果子句未选中单选按钮,则else子句工作并在数据库中插入数据。”else子句将起作用“您没有
else
RadioButtonList\u gorusmeYapilanOkul的值是多少。调试时SelectedValue
?我按照您说的做了。首先选择一个单选按钮列表项,如果子句正确显示所选值,则使用按钮启动。但是lbl\u hata.Text未设置为“not Empty!”!!“。出现了一些问题,但我无法理解。请确认是否在两行lbl_-hata.Text上都设置了断点,哪一行正在运行?lbl_-hata文本是否发生了任何更改?请首先尝试将初始值设置为类似于‘not run’的值。