Solved

repeater control

Posted on 2007-11-17
5
704 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
Comment Utility
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
Comment Utility
and this can be worked right into a Repeater control??
0
 
LVL 16

Accepted Solution

by:
anoyes earned 20 total points
Comment Utility
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
Comment Utility
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
Comment Utility
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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

762 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

12 Experts available now in Live!

Get 1:1 Help Now