簡體   English   中英

如何檢查單選按鈕列表是否被選中

[英]how to check radiobuttonlist selected or not

我是 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>

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";
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM