how to check radiobuttonlist selected or not

18k views Asked by At

I'm new to asp.net and c#. Trying to check radiobutton is selected or not selected. if it selected else clause will work if it's not selected want to set lbl_hata with "EMPTY!!" text.

I have also tried selectedIndexChanged and it didn't work. How can I solve this situation?

if (RadioButtonList_gorusmeYapilanOkul.SelectedValue == "")
{
    lbl_hata.Text = "EMPTY!!";
}
if (yetkiliAdSoyad_txt.Text=="")
{
    lbl_hata.Text = "EMPTY!!";
}

This is aspx page;

<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

There are 1 answers

2
mattumotu On

selectedIndexChanged is an event that will be fired (called) when the user selects a radio button in the group.

It should like you are trying to check whether RadioButtonList_gorusmeYapilanOkul has a selected option or not. I assume this is on a different event (like the user has clicked on a button). If this is the case you don't need to trap the selectedIndexChanged event.

I would suggest you use some code like the following:

if (RadioButtonList_gorusmeYapilanOkul.SelectedValue == "")
{
    lbl_hata.Text = "EMPTY!!";
}
else
{
    lbl_hata.Text = "Not empty!!";
}

Then put a breakpoint on the first line and walk through using the debugger, this will allow you to get an understanding of what is happening and how the logic flows.

UPDATE: I don't want to post this as a comment because it would lose formatting. Can you try the following code:

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