Home > ASP.Net > Display Images in RadioButtonList Control in ASP.NET

Display Images in RadioButtonList Control in ASP.NET


Hi all,

Try this example to Display Images in RadioButtonList Control in ASP.NET

You can also check my previous posts

Bind RadioButtonList with Image from Database

Bind RadioButtonList to Image

Step 1: Create a new ASP.NET Web Application in Visual Studio. Add a folder in the root named Images. Right-click on the project name and select:

Add -> New Folder

Now add some images in this folder to show with RadioButtons in the RadioButtonList control. Right-click on the Images folder and select:

Add -> Existing Item

Application-Folder-in-ASP.NET.jpg

Step 2: Add a RadioButtonList in HTML source view of Default.aspx, as shown in the following:

<div>
<asp:RadioButtonList ID="RadioButtonList1" runat="server"
BorderStyle="Groove" BorderWidth="1px" RepeatColumns="3" RepeatLayout="Table">
</asp:RadioButtonList>
</div>

RepeatColumns of RadioButtonList is set to “3” to divide its items in three columns. For example if we have 18 items in the RadioButtonList, it will be displayed in 3 columns with 6 RadioButtons in each column.
The RepeatLayout property is used to specify the arrangement of RadioButtons inside the RadioButtonList control. By default or when it is set to “Table”, RadioButtons are rendered in a table but if it is set to “Flow”, it is rendered without a table.

Step 3: Write the following code in the Page Load event of Default.aspx.cs:

protected void Page_Load(object sender, EventArgs e)
{
ListItem item;
int i = 0;
System.IO.FileInfo file;

var Images =
from n in System.IO.Directory.GetFiles(Server.MapPath("Images"))
orderby n descending
select n;

foreach (var filename in Images)
{
file = new System.IO.FileInfo(filename);

item = new ListItem("<img src='" + "Images/" + file.Name + "' alt='" + file.Name +
"' title='"+file.Name+"'/>", i.ToString());

RadioButtonList1.Items.Add(item);
RadioButtonList1.CellPadding = 5;
RadioButtonList1.CellSpacing = 5;
i++;
}
}

Hope this helps

Good Luck.

 

Advertisements
Categories: ASP.Net
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: