根据从数据库结果中检索的值设置单选按钮选择值?

Set radio button selected value according to the value retrieved from database result?

 <asp:RadioButtonList ID="rbnPropertyType" runat="server" RepeatDirection="Horizontal" RepeatLayout="Flow">
    <asp:ListItem Text="Commercial"   Value="1" />
     <asp:ListItem Text="Residential" Value="2" />

这是 ma aspx 代码。 .cs 代码是

? = dtPolicyInfo.Rows[0]["PropertyType"].ToString();

如何根据数据库结果设置单选按钮的选中值?

首先,清除 selected 列表:

rbnPropertyType.SelectedIndex = -1;

现在按值或文本搜索项目,然后 select 它。

rbnPropertyType.Items.FindByValue(dtPolicyInfo.Rows[0]["PropertyType"].ToString()).Selected = true;
rbnPropertyType.Items.FindByText(dtPolicyInfo.Rows[0]["PropertyType"].ToString()).Selected = true;
string PropertyTypeID= dtPolicyInfo.Rows[0]["PropertyType"].ToString();
rbnPropertyType.Items.FindByText(PropertyTypeID).Selected = true;

试试这个可能会有帮助:

rbnPropertyType.ClearSelection();
 rbnPropertyType.SelectedIndex = rbnPropertyType.Items.IndexOf(rbnPropertyType.Items.FindByValue(dtPolicyInfo.Rows[0]["PropertyType"].ToString()));