Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

So I have a repeater control and everything displays as expected. My situation is that I have a field (websiteURL) that may or may not have data. If there is data I want to display the field in an HTML <img> and <a> tag. Like this:

<a target="_blank" 
                           href="<%#DataBinder.Eval(Container.DataItem, "LocURL")%>">
                       <img border="0" src="images\icons\Website-Icon.gif"
                        alt="<%#DataBinder.Eval(Container.DataItem, "LocURL")%>" /></a>

The problem is that when there is no data in the field my image link is displayed, but links to a blank page.

 

Basically what I want to do is this:

 

<%
                       If String.IsNullOrEmpty(#DataBinder.GetPropertyValue(Container.DataItem, "LocURL").ToString()) = True Then

%>
<a target="_blank" 
                           href="<%#DataBinder.Eval(Container.DataItem, "LocURL")%>">
                       <img border="0" src="images\icons\Website-Icon.gif"
                        alt="<%#DataBinder.Eval(Container.DataItem, "LocURL")%>" /></a>

<%
                           End If
%>
                       

The problem there is that I cannot use the #Container.DataItem in the context of an If statement.

 

What is the easiest way to use the repeater control and check to make sure data exists in that field before displaying it?

~Nate�

___________________________________________

Please use the [vb]/[cs] tags on posted code.

Please post solutions you find somewhere else.

Follow me on Twitter here.

  • 1 month later...
Posted

Sorry for the bump, but rather than repost I thought I'd supply a bit of additional information. Basically what I'm trying to do, is determine if there is data in this field, if yes, display an image, otherwise, just leave white space.

 

Thanks in advance!

~Nate�

___________________________________________

Please use the [vb]/[cs] tags on posted code.

Please post solutions you find somewhere else.

Follow me on Twitter here.

Posted (edited)

Why not use the DataBinding event of the Repeater object? I would. :D

 

You can check the values inside each data row and then decide what to do for each row.

Edited by amir100
Amir Syafrudin
Posted
Have you got an example or some pseudo code? I'm not sure where to get started.

~Nate�

___________________________________________

Please use the [vb]/[cs] tags on posted code.

Please post solutions you find somewhere else.

Follow me on Twitter here.

Posted

Wrong information.

 

I'm suppose to suggest you to use the ItemDataBound event of the Repeater control.

 

Here's a sample code I got from MSDN 2005.

 

[csharp]

<%@ Page Language="C#" AutoEventWireup="True" %>

<html>

<head>

<script language="C#" runat="server">

void Page_Load(Object Sender, EventArgs e) {

 

if (!IsPostBack) {

ArrayList values = new ArrayList();

 

values.Add(new Evaluation("Razor Wiper Blades", "Good"));

values.Add(new Evaluation("Shoe-So-Soft Softening Polish", "Poor"));

values.Add(new Evaluation("DynaSmile Dental Fixative", "Fair"));

 

Repeater1.DataSource = values;

Repeater1.DataBind();

}

}

 

void R1_ItemDataBound(Object Sender, RepeaterItemEventArgs e) {

 

// This event is raised for the header, the footer, separators, and items.

 

// Execute the following logic for Items and Alternating Items.

if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem) {

 

if (((Evaluation)e.Item.DataItem).Rating == "Good") {

((Label)e.Item.FindControl("RatingLabel")).Text= "<b>***Good***</b>";

}

}

}

 

public class Evaluation {

 

private string productid;

private string rating;

 

public Evaluation(string productid, string rating) {

this.productid = productid;

this.rating = rating;

}

 

public string ProductID {

get {

return productid;

}

}

 

public string Rating {

get {

return rating;

}

}

}

 

</script>

 

</head>

<body>

 

<h3>OnItemDataBound Example</h3>

 

<form runat=server>

 

<p>

<asp:Repeater id=Repeater1 OnItemDataBound="R1_ItemDataBound" runat="server">

<HeaderTemplate>

<table border=1>

<tr>

<td><b>Product</b></td>

<td><b>Consumer Rating</b></td>

</tr>

</HeaderTemplate>

 

<ItemTemplate>

<tr>

<td> <asp:Label Text='<%# DataBinder.Eval(Container.DataItem, "ProductID") %>' Runat="server"/> </td>

<td> <asp:Label id=RatingLabel Text='<%# DataBinder.Eval(Container.DataItem, "Rating") %>' Runat="server"/> </td>

</tr>

</ItemTemplate>

 

<FooterTemplate>

</table>

</FooterTemplate>

 

</asp:Repeater>

<p>

 

</form>

</body>

</html>

[/csharp]

 

As quoted from the same source, the event "occurs after an item in the Repeater is data-bound but before it is rendered on the page".

 

That way you can check the values in every row and then manipulate it as you wish.

 

In the sample code. The event handler sample for ItemDataBound event is R1_ItemDataBound. You can use the "e" parameter to access each row in the Repeater.

 

Hope it helps. Sorry for the late reply.

Amir Syafrudin

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...