C# 根据从数据库读取的值检查RadioButtonList中的单选按钮

C# 根据从数据库读取的值检查RadioButtonList中的单选按钮,c#,asp.net,radiobuttonlist,C#,Asp.net,Radiobuttonlist,我有一个RadioButtonList,我想根据从数据库读取的值在此列表中选择一个单选按钮。从数据库中读取值后,如何使列表中的单选按钮变为选中状态?您只需通过RadioButtonList.SelectedValue=reader.value只要您知道该值在列表中(如果它不在列表中,那么当该行执行时,您将得到一个异常) 由于听起来您不确定reader.value将是RadioButtonList中的选项之一,因此您需要首先检查该选项 if(RadioButtonList.Items.FindBy

我有一个RadioButtonList,我想根据从数据库读取的值在此列表中选择一个单选按钮。从数据库中读取值后,如何使列表中的单选按钮变为选中状态?

您只需通过
RadioButtonList.SelectedValue=reader.value只要您知道该值在列表中(如果它不在列表中,那么当该行执行时,您将得到一个异常)

由于听起来您不确定
reader.value
将是
RadioButtonList
中的选项之一,因此您需要首先检查该选项

if(RadioButtonList.Items.FindByValue(reader.value) != null) {
    RadioButtonList.SelectedValue = reader.value;
}
或者,您可以通过try/catch处理异常

string sSortname = row["GoodsSortName"].ToString().Trim();
        foreach (ListItem s in this.rdbSort.Items)
        {
            if (s.Text == sSortname)
            {
                s.Selected = true;
                break;
            }

        }

我 用这种方法,解决了这个问题

实际上,RadioButtonlist.value是硬编码。数据库仅存储文本。因此无法使用rdb.selectedvalue=reader.value