Lets say I have a data table with the columns ImageUrl, Text, LinkUrl and Title.
I databind this to a repeater and displays everything without any issue. However, LinkUrl can be empty and then I want the code to be like the one below. If the LinkUrl contains a string/link then I want to wrap the image with a link to the URL. Please ask questions if I need to clarify more.
Example code:
<asp:Repeater runat="server" ID="rptImageGallery">
<ItemTemplate>
<div class="slide">
<asp:Image ID="Image1" runat="server" ImageUrl='<%#UrlHelper(DataBinder.Eval(Container.DataItem, "ImageUrl"))%>'>
<h2><%# DataBinder.Eval(Container.DataItem, "Title") %></h2>
<p><%# DataBinder.Eval(Container.DataItem, "Text")%></p>
</div>
</ItemTemplate>
</asp:Repeater>
Code behind:
DataTable dt = new DataTable();
dt = collListItems.GetDataTable().AsEnumerable()
.Where(a => Convert.ToString(a["Published"]) == "1")
.CopyToDataTable();
rptImageGallery.DataSource = dt;
rptImageGallery.DataBind();
So if the current row contains a link in LikUrl then I want the repeater to present something like this
<asp:Repeater runat="server" ID="rptImageGallery">
<ItemTemplate>
<div class="slide">
<asp:Image ID="Image1" runat="server" ImageUrl='<%# UrlHelper(DataBinder.Eval(Container.DataItem, "FileLeafRef"))%>' />
<a href='<%# DataBinder.Eval(Container.DataItem, "LinkUrl")%>' runat="server"><%# DataBinder.Eval(Container.DataItem, "Title") %></h2></a>
<p><%# DataBinder.Eval(Container.DataItem, "Text")%></p>
</div>
</ItemTemplate>
</asp:Repeater>
Any suggestions on how to achieve this?
Edit: something weird with the post. When I go Edit I can see the correct code – slick Save and it doesn’t update my code example. Anyway, in the last code part I want a link wrapping the image or image text.
try this:
code behind (I have added complete code so that you can test):
PS:- probably you will not need
GetUrlfunction.