ayha
ayha

Reputation: 181

Setting RadioButtonList selected value

I am trying to select the radiobuttonlist's using the following code but it never get selected.

<asp:RadioButtonList ID="rdBond" CssClass="RadioButtonList"
                            runat="server" RepeatDirection="Horizontal">
                            <asp:ListItem>Yes</asp:ListItem>
                            <asp:ListItem>No</asp:ListItem>
                        </asp:RadioButtonList>

and I tried both of the follwing methods but doesn't work.

       ListItem l2 = rdOffset.Items.FindByValue(cd.BondReq.ToString());
        if (l2 != null)
            l2.Selected = true;

        rdOffset.SelectedValue = cd.Offset.ToString();

Please help to fix it.

Upvotes: 1

Views: 7897

Answers (3)

Rohit Sharma
Rohit Sharma

Reputation: 379

use like this

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

Upvotes: 1

Suji
Suji

Reputation: 1326

Make this change :

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

Upvotes: 2

Tdorno
Tdorno

Reputation: 1571

Per this question, you should use values when dealing with list items.

<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

Upvotes: 0

Related Questions