Adding image to a radio button list in ASP.Net

18k views Asked by At

I am trying to add an image to a radio button list control but its not working..

I tried this..

RadioButtonList2.Items.Add(new ListItem(String.Format("src='../Colors/Dallas_#625527_1.1.png'>")));

but the whole image tag appears as text

I tried I design time as well

 <asp:RadioButtonList ID="rbListImages" runat="server">
    <asp:ListItem Text="One" Value="1"><img src="../Colors/Dallas_#625527_1.1.png" alt="" /></asp:ListItem>
</asp:RadioButtonList>

but it says img tag cant be nested with the listitem tag.. Please help me out..

2

There are 2 answers

0
Rahul Singh On BEST ANSWER

You need to specify the control control, you are trying to set the src tag but there is no image control. Try this:-

RadioButtonList2.Items.Add(new ListItem("<img src='"+"../Colors/Dallas_#625527_1.1.png"+"'/>"));

You can also add this at design time, like this:-

<asp:RadioButtonList ID="imagetest" runat="server">
         <asp:ListItem Text='<img src="Image1.jpg" alt="img1" />' Value="1" Selected="True" />
         <asp:ListItem Text='<img src="Image2.jpg" alt="img2" />' Value="2"></asp:ListItem>
</asp:RadioButtonList>
0
Manish Goswami On
<asp:RadioButtonList ID="RadioButtonList1" runat="server" >
<asp:ListItem Text="&lt;img src=&quot;Images/AddIcon.gif&quot;/&gt; Option1" Value="1">   </asp:ListItem>

  <asp:ListItem Text="&lt;img src=&quot;Images/1.gif&quot;/&gt; Option2" Value="2"></asp:ListItem>

  <asp:ListItem Text="&lt;img src=&quot;Images/2.gif&quot;/&gt; Option3" Value="3"></asp:ListItem>

 <asp:ListItem Text="&lt;img src=&quot;Images/3.gif&quot;/&gt; Option4" Value="4"></asp:ListItem>

(OR) ANother way : From Code Behind : Display Images in RadioButtonList Control