![](/img/trans.png)
[英]Setting selected value of RadioButtonList does not work as expected
[英]Setting RadioButtonList selected value
我正在尝试使用以下代码选择单选按钮列表,但从未被选中。
<asp:RadioButtonList ID="rdBond" CssClass="RadioButtonList"
runat="server" RepeatDirection="Horizontal">
<asp:ListItem>Yes</asp:ListItem>
<asp:ListItem>No</asp:ListItem>
</asp:RadioButtonList>
并且我尝试了以下两种方法,但是没有用。
ListItem l2 = rdOffset.Items.FindByValue(cd.BondReq.ToString());
if (l2 != null)
l2.Selected = true;
rdOffset.SelectedValue = cd.Offset.ToString();
请帮助修复它。
进行更改:
<asp:RadioButtonList ID="rdBond" CssClass="RadioButtonList"
runat="server" RepeatDirection="Horizontal">
<asp:ListItem Selected =True >Yes</asp:ListItem> <%--change this in your code--%>
<asp:ListItem>No</asp:ListItem>
</asp:RadioButtonList>
这样使用
<asp:RadioButtonList ID="rdBond" runat="server">
<asp:ListItem Text="Yes" Value="1"></asp:ListItem>
<asp:ListItem Text="No" Value="2"></asp:ListItem>
</asp:RadioButtonList>
// in code file
rdBond.SelectedValue = "1";
对于这个问题 ,在处理列表项时应使用values
。
<asp:RadioButtonList ID="rdBond" CssClass="RadioButtonList" runat="server" RepeatDirection="Horizontal">
<asp:ListItem value="yes"/>
<asp:ListItem value="no"/>
</asp:RadioButtonList>
ListItem A = rdOffset.Items.FindByValue("yes");
ListItem B = rdOffset.Items.FindByValue("no");
var a = A.Selected
var b = B.Selected
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.