Solved

repeater control

Posted on 2007-11-17
5
709 Views
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:

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

Expert Comment

by:anoyes
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

0
 

Author Comment

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

Accepted Solution

by:
anoyes earned 20 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.
0
 

Author Comment

by:geoffsweb
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");

ideas?

I really appreciate the help....
0
 

Author Comment

by:geoffsweb
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 !!
0

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
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.
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

810 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