Anh Nguyen
Anh Nguyen

Reputation: 313

How to get value of radio in datalist?

I have a data list like this :

<asp:DataList ID="DataList1" runat="server" BackColor="White" BorderColor="#CC9966"
        BorderStyle="None" BorderWidth="1px" CellPadding="4" GridLines="Both" RepeatColumns="3"
        onitemdatabound="DataList1_ItemDataBound">
        <FooterStyle BackColor="#FFFFCC" ForeColor="#330099" />
        <HeaderStyle BackColor="#990000" Font-Bold="True" ForeColor="#FFFFCC" />
        <ItemStyle BackColor="White" ForeColor="#330099" />
        <ItemTemplate>
            <table>
                <tr>
                    <td style="width:10px">
                        <asp:RadioButton ID="rd_CS" runat="server" GroupName="Casi" 
                         oncheckedchanged="rd_CS_CheckedChanged" Text='<%# Eval("Key")%>'></asp:RadioButton>
                    </td>
                    <td>
                        <asp:Image ID="Img_Nhacsi" runat="server" ImageUrl='<%#Eval("Hinhanh")%>' Width="75px"
                            Height="75px" BorderColor="#66FF33" />
                    </td>
                    <td>
                        <asp:Label ID="lbl_Ten" runat="server" Text='<%#Eval("Name")%>'></asp:Label>
                    </td>
                    <td>
                        <asp:Label ID="lbl_Ngaysinh" runat="server" Text='<%#Eval("Birthdate")%>'></asp:Label>
                    </td>
                    <td>
                        <asp:Label ID="Label2" runat="server" Text='<%#Eval("Country")%>'></asp:Label>
                    </td>
                </tr>
            </table>
        </ItemTemplate>
        <SelectedItemStyle BackColor="#FFCC66" Font-Bold="True" ForeColor="#663399" />
    </asp:DataList>

I got a button submit in page. How to get the text of checked radio button when i click submit button ? Any help would be great.

Upvotes: 1

Views: 1243

Answers (2)

afzalulh
afzalulh

Reputation: 7943

Probably you need something like this (Credit goes to Guffa):

string myRadioText = String.Empty;

foreach (DataListItem item in DataList1.Items)    
{    
    RadioButton rd_CS = (RadioButton)item.FindControl("rd_CS");

    if(rd_CS != null && rd_CS.Checked)
    {
        myRadioText = rd_CS.Text;
    }    
}

Upvotes: 0

Guffa
Guffa

Reputation: 700152

You use Request.Form["rdGroup"] to get the value.

If you want to get it in the context of the data list, then that's not possible with the current code. You need to add runat="server" to the radio button, then you can loop though the items and use the Find method to get a reference to the radio button control.

Upvotes: 1

Related Questions