• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 715
  • Last Modified:

repeater control

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
geoffsweb
Asked:
geoffsweb
  • 3
  • 2
1 Solution
 
anoyesCommented:
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
 
geoffswebAuthor Commented:
and this can be worked right into a Repeater control??
0
 
anoyesCommented:
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
 
geoffswebAuthor Commented:
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
 
geoffswebAuthor Commented:
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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now