Solved

repeater control

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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …

828 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