Solved

Create A RadiobuttonList with Images Asp.net 2.0

Posted on 2009-06-29
12
334 Views
Last Modified: 2012-06-27
I am populating a radiobuttonlist from my database. I would like to add images by each radiobutton populated. Can someone show me how to do this?
0
Comment
Question by:npl77
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 6
12 Comments
 
LVL 41

Expert Comment

by:guru_sami
ID: 24739067
How are you dataBinding your RBL?
Here is a link trying to do that:
http://aspdotnetcodebook.blogspot.com/2008/09/howto-display-radiobuttonlist-with.html

Other option is to use a DataBound Control like GV/Repeater and place a RadioButton and an Image Control.
0
 

Author Comment

by:npl77
ID: 24739621
Im not sure how to implement the example in the url. This is how I am populating the radiolist...

 if (!IsPostBack)
            {
                dt = BMHAccess.GetProducts();
                PopulateList(RadioButtonList1, dt);
               
            }
 
private void PopulateList(RadioButtonList list, DataTable products)
    {
        list.DataSource = products;
        list.DataTextField = "Name";
        list.DataValueField = "ProductId";
        list.DataBind();
 
    }

Open in new window

0
 
LVL 41

Expert Comment

by:guru_sami
ID: 24739713
From where does the image come?
i.e. how do you get url to your image?
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:npl77
ID: 24739875
I have the images folder in my application. I dont know if I should store them somehow in my db or what
0
 
LVL 41

Accepted Solution

by:
guru_sami earned 500 total points
ID: 24740381
Yes ..you will need some mapping between the images and corresponding products.
So say you have a column imageFileName in your DataTable then your PopulateList function should look like this:
 private void PopulateList(RadioButtonList list, DataTable dt)
    {
        for (int i = 0; i < dt.Rows.Count; i++)
        {
           // here 'images' corresponds to the folder in your application
            string imgsrc = "images/" + dt.Rows[i]["imageFileName"].ToString();;
            string value = dt.Rows[i]["ProductId"].ToString();
            list.Items.Add(new ListItem(String.Format("<img src=\"{0}\">", imgsrc), value));
        }
    }

Note: you cannot display the Name here.
0
 

Author Comment

by:npl77
ID: 24740566
I cannot display the name? how can I display the name? I want it to look....
image "space" [ProductName]
0
 

Author Comment

by:npl77
ID: 24740660
ok I almost got it!
this displays correctly
 list.Items.Add(new ListItem(String.Format("<img src=\"{0}\">" +  " "+dt.Rows[i]["Name"].ToString(), imgsrc), value));

but now my string manipulation is wrong... because of this ("<img src=\"{0}\"> stuff can you help me fix this string manipulation to correspond witn what I got


        string sFormat = RadioButtonList1.SelectedItem.ToString();
        string[] nameprice;
        nameprice = sFormat.Split('-');
 
        btnTodaysSelections.ItemName = nameprice[0].TrimEnd();
        btnTodaysSelections.ItemNumber = RadioButtonList1.SelectedValue;
        btnTodaysSelections.Amount = decimal.Parse(nameprice[1].Substring(2));

Open in new window

0
 
LVL 41

Expert Comment

by:guru_sami
ID: 24740668
Is the above code working?
0
 
LVL 41

Expert Comment

by:guru_sami
ID: 24740685
Try like this ...
list.Items.Add(new ListItem(String.Format("<img src=\"{0}\">", imgsrc) + dt.Rows[i]["Name"].ToString(), value));
0
 

Author Comment

by:npl77
ID: 24740775
the data the SelectedItem is giving me is now...
<img src="images/AnImage1.gif"> Monthly Subscription - $29.95

I need string manipulation code to get rid of <img src="images/AnImage1.gif">
0
 

Author Closing Comment

by:npl77
ID: 31598014
I got it thank you
0
 
LVL 41

Expert Comment

by:guru_sami
ID: 24740874
oh i see what you mean:
try this:
     char[] ch = {'>','-'};
       string[] s1 = s.Split(ch);      
        string itemname = s1[1];
        string amount = s1[2];
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article discusses the ASP.NET AJAX ModalPopupExtender control. In this article we will show how to use the ModalPopupExtender control, how to display/show/call the ASP.NET AJAX ModalPopupExtender control from javascript, how to show/display/cal…
ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…
This video shows how to use Hyena, from SystemTools Software, to update 100 user accounts from an external text file. View in 1080p for best video quality.

738 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question