Solved

repeater control

Posted on 2007-11-17
5
707 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
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.
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…

911 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now