If condition in ASP.NET Repeater
I'm having a bit of trouble with my ASP.NET Repeater control. I need to show some HTML depending on a boolean property on the object in the collection that the repeater is bound 开发者_开发问答to.
I have this code:
<asp:Repeater ID="rptListPartners" runat="server" OnItemDataBound="rptListPartners_ItemDataBound">
<HeaderTemplate>
<table border="0" cellpadding="7" cellspacing="0">
</HeaderTemplate>
<ItemTemplate>
<tr>
<td style="font-family:Verdana; font-size:11px; height: 18px; width:370px;">
<div id="data_tooltip_from_div<%#Eval("id") %>" style="display:none; z-index:10;"><%#Eval("profile") %></div>
<a id="tooltip_from_div<%#Eval("id") %>" href='<%#Eval("homepage") %>' class="tooltip" target="_blank">
<div style="float:left;"><%#Eval("name") %>, <%#Eval("address") %>, <%#Eval("zip") %> <%#Eval("city") %> </div>
<div style="width:18px; float:left;">
<asp:Panel Width="18px" ID="pnlLink" runat="server" Visible='<%#Eval("IsUrl") %>'>
<a href='<%#Eval("homepage") %>' target="_blank">
<img src='/kort/www.png' />
</a>
</asp:Panel>
</div>
</a>
</td>
<td style="font-family:Verdana; font-size:11px; height: 18px;">Tlf. <%#Eval("phone") %></td>
</tr>
</ItemTemplate>
<FooterTemplate>
</table>
</FooterTemplate>
I need to display the
<a id="tooltip_from_div<%#Eval("id") %>" .... >
</a>
tag with a span or div tag instead if the <%#Eval("IsUrl") %> is false - thus not displaying a link if there is no url string on the object.
How exactly do I go about this? Any help/hint is greatly appreciated! :-)
All the best,
Bo
You could try by placing both a
and div
tags, then set the Visible
property according to your condition.
Simple, easy, smart
Hmm, I solved it myself by doing this:
On the .aspx page I'm calling a method to write out the html:
<asp:Repeater ID="rptListPartners" runat="server" OnItemDataBound="rptListPartners_ItemDataBound">
<HeaderTemplate>
<table border="0" cellpadding="7" cellspacing="0">
</HeaderTemplate>
<ItemTemplate>
<tr>
<td style="font-family:Verdana; font-size:11px; height: 18px; width:370px;">
<div id="data_tooltip_from_div<%#Eval("id") %>" style="display:none; z-index:10;"><%#Eval("profile") %></div>
<%# CheckUrl(DataBinder.Eval(Container.DataItem, "IsUrl"),
DataBinder.Eval(Container.DataItem, "id"),
DataBinder.Eval(Container.DataItem, "homepage"),
DataBinder.Eval(Container.DataItem, "name"),
DataBinder.Eval(Container.DataItem, "address"),
DataBinder.Eval(Container.DataItem, "zip"),
DataBinder.Eval(Container.DataItem, "city"))
%>
</td>
<td style="font-family:Verdana; font-size:11px; height: 18px;">Tlf. <%#Eval("phone") %></td>
</tr>
</ItemTemplate>
<FooterTemplate>
</table>
</FooterTemplate>
</asp:Repeater>
Then in my codebehind:
public string CheckUrl(object isUrl, object id, object homepage, object name, object address, object zip, object city)
{
string html = string.Empty;
if (isUrl != null)
{
string _id = id.ToString();
string _homepage = homepage.ToString();
string _name = name.ToString();
string _address = address.ToString();
string _zip = zip.ToString();
string _city = city.ToString();
if (isUrl.ToString().Equals("True"))
{
html = "<a id='tooltip_from_div" + _id + "' href='" + _homepage + "' class='tooltip' target='_blank'>"
+ "<div style='float:left;'>" + _name + ", " + _address + ", " + _zip + " " + _city + " </div>"
+ "<div style='width:18px; float:left;'> "
+ " <asp:Panel Width='18px' ID='pnlLink' runat='server' Visible='" + isUrl.ToString() + "'>"
+ " <a href='" + _homepage + "' target='_blank'>"
+ " <img src='/kort/www.png' />"
+ " </a>"
+ " </asp:Panel>"
+ "</div>"
+ "</a>";
}
else
{
html = "<span id='tooltip_from_div" + _id + "' class='tooltip' target='_blank'>"
+ "<div style='float:left;'>" + _name + ", " + _address + ", " + _zip + " " + _city + " </div>"
+ "</span>";
}
}
return html;
}
Works like a charm :-)
djechelon: I tried your suggestion, but I simply couldn't get it to work with all the javascript going on aswell and such :-( Sorry..
精彩评论