Repeater Woes

F

Fao, Sean

I'm trying to edit the data as it is being bound in an ASP.NET 1.1
Repeater, but I'm having no luck.

The relevant portion of my template is pasted below:

<tr>
<td colspan="4">
<asp:Label id="CommentsLabel" runat="server">
<%# DataBinder.Eval(Container.DataItem, "Comments") %>
</asp:Label></td>
</tr>

Basically, if the length of the "Comments" column is longer than
MaxCommentsLength, I'd like to chop off the end and attach "..." at the
end of the string. I attempted this with success using a DataGrid by
editing the string in the ItemDataBound event handler. However, I
noticed that with the Repeater control, the Data is not yet bound when
the ItemDataBound event is fired, so I have no way to determine what the
original value is so that I can modify it (at least not in the event
handler for the ItemDataBound event).

I tried the following code:

private void SoftDeletedCWMRepeater_ItemCreated(object sender,
System.Web.UI.WebControls.RepeaterItemEventArgs e)
{
Label lbl;
int MaxCommentsLength = 15; // To be moved to web.config

if (e.Item.ItemType == ListItemType.Item ||
e.Item.ItemType == ListItemType.AlternatingItem)
{
if ((lbl = (Label) e.Item.FindControl("CommentsLabel")) != null)
{
if (lbl.Text.Length > MaxCommentsLength)
lbl.Text = lbl.Text.Substring(0, MaxCommentsLength) + "...";
} // end if
} // end if
} // end event handler

Using a debugger, I determined that the "Text" property of (Label)
e.Item.FindControl("CommentsLabel") was not yet set in the ItemCreated
event handler, which makes this code not work.

What are my options?

Thank you in advance,
 
K

Karl Seguin [MVP]

<asp:Label id="CommentsLabel" runat="server" />

in ItemsDataBound you can get the data that is going to get bound, via
e.Item.DataItem

so you can do
Label lbl = (Label) e.Item.FindControl("CommentsLabel");
string comments= DataBinder.Eval(e.Item.DataItem, "Comments");
if (comments.Length > XXX)
{
lbl.Text = comments.Substring(0, MaxCommentsLength) + "...";
}
else
{
lbl.Text = comments;
}

Karl
 
P

Peter Rilling

With stuff like this, I usually create a helper method where I pass in the
original value and pass out the modified value.

You may have to tweak this as I am not compiling it but the general idea
would be as follows:

IN-CODE:
public static TruncateContent(string content){
if(content.Length != 200){
// truncate and return the shortend value.
}

return content;
}

IN-ASPX:
<%# MyUtilityClass.TruncateContent( DataBinder.Eval(Container.DataItem,
"Comments") ) %>
 
F

Fao, Sean

Peter said:
With stuff like this, I usually create a helper method where I pass in the
original value and pass out the modified value.

That's a great idea! Thank you very much!
 
F

Fao, Sean

Karl said:
<asp:Label id="CommentsLabel" runat="server" />

in ItemsDataBound you can get the data that is going to get bound, via
e.Item.DataItem

Interesting. I could have sworn that I had tried that.

Thank you very much for your response,
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top