repeater control

Posted on 2007-11-17
Medium Priority
Last Modified: 2012-06-21
I'm using  a repeater control in my ASP.NET page using C#.  Within that repeater control, i want to have an image show depending on a certain value pulled back from the database.  Not sure how to do that?
(my repeater control works)
  Here's my code below:

                        <td><b>Year:</b> <%# DataBinder.Eval(Container.DataItem,"EventYear") %></td>
                        <td><b>Name:</b>  <%# DataBinder.Eval(Container.DataItem,"Name") %></td>
                        <td><b>Email Address:</b> <%# DataBinder.Eval(Container.DataItem,"EmailAddress") %></td>
                        <td><b>Course: // this is where I want a certain image to go depending on value in db</b></td>
                        <td><b>Organization: // this is where I want a certain image to go depending on value in db</b></td>
                        <td><b>Pros:</b><%# DataBinder.Eval(Container.DataItem,"Pros") %></td>
                        <td><b>Cons:</b> <%# DataBinder.Eval(Container.DataItem,"Cons") %></td>
                        <td><b>Comments:</b><%# DataBinder.Eval(Container.DataItem,"Comments") %></td>
                        <td><hr /></td>
Question by:geoffsweb
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
  • 3
  • 2
LVL 16

Expert Comment

ID: 20305066
First, put image controls in where you want the images to be displayed.  In my sample, they have IDs "imgCourse" and "imgOrg".  Don't supply an ImageUrl.  Then in code behind, put in the code.  I wrote this up in VB and then converted to C#, so let me know if it doesn't work right, but that's the general idea.  It will run this for each row, get the value from the DB, in this case the field is "FieldToCheck", and then does a case check..you can change it for true/false, etc.

# protected void SelectImages(object sender, RepeaterItemEventArgs e)
# {
#     if (e.Item.ItemType == ListItemType.Item | e.Item.ItemType == ListItemType.AlternatingItem) {
#         string DBValue = DataBinder.Eval(e.Item.DataItem, "FieldToCheck").ToString;
#         Image imgCourse = e.Item.FindControl("imgCourse");
#         Image imgOrg = e.Item.FindControl("imgOrg");
#         switch (DBValue) {
#             case "1":
#                 imgCourse.ImageUrl = "course1.jpg";
#                 imgOrg.ImageUrl = "org1.jpg";
#                 break;
#             case "2":
#                 ..etc();
#                 break;
#         }
#     }
# }

Open in new window


Author Comment

ID: 20305085
and this can be worked right into a Repeater control??
LVL 16

Accepted Solution

anoyes earned 80 total points
ID: 20305314
Yup, just goes in the codebehind for you page, or gets wrapped in a <script runat="server"></script> tag on the page itself.  Those # symbols need to come out..don't know how they got in there.  The one thing I forgot was that you have to add OnItemDataBound="SelectImages" to the <asp:Repeater> tag.

Author Comment

ID: 20305460
ok, I think i'm getting close (well, you are !).... I"ve got it all set up, but when I compile it, I get this error:

Cannot implicitly convert type 'System.Web.UI.Control' to 'System.Web.UI.WebControls.Image'. An explicit conversion exists (are you missing a cast?)

on this line:
Image imgCourse = e.Item.FindControl("imgCourse");


I really appreciate the help....

Author Comment

ID: 20305968
figured it out... was missing the (image) in front of e.Item.FindControl("imgCourse");
so it should have been:
Image imgCourse = (Image)e.Item.FindControl("imgCourse");
thanks for the help !!

Featured Post

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!

Question has a verified solution.

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

We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…
Suggested Courses

650 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