returning x chars of Eval("..")

K

Kevin Blount

I'm putting a radG:GridTemplateColumn together (which is probably
irelevant), and within it I'm using a Label, as so:

<asp:Label ID="defaultDescription" runat="server" Text='<%#
Eval("description") %>'></asp:Label>

Fo this Label, I'd like to only show the first 50 chars of the
"description", but I've no idea how to change the Eval to do this.. is
there a way?
 
N

Nicholas Paldino [.NET/C# MVP]

Kevin,

The Eval method returns an object, which you can call ToString on and
then call substring on:

<%# Eval("description").ToString().Substring(0, 50) %>

Or, assuming description is a string field (and you know this), you
could just cast:

<% ((string) Eval("description")).Substring(0, 50) %>
 
M

Mythran

Nicholas Paldino said:
Kevin,

The Eval method returns an object, which you can call ToString on and
then call substring on:

<%# Eval("description").ToString().Substring(0, 50) %>

Or, assuming description is a string field (and you know this), you
could just cast:

<% ((string) Eval("description")).Substring(0, 50) %>


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Kevin Blount said:
I'm putting a radG:GridTemplateColumn together (which is probably
irelevant), and within it I'm using a Label, as so:

<asp:Label ID="defaultDescription" runat="server" Text='<%#
Eval("description") %>'></asp:Label>

Fo this Label, I'd like to only show the first 50 chars of the
"description", but I've no idea how to change the Eval to do this.. is
there a way?

<asp:Label ID="defaultDescription" runat="server" Text='<%#
Eval("description") == null ? string.Empty : ((string)
Eval("description")).Substring(0, 50) %>

just to be on the safe side :p

Mythran
 
K

Kevin Blount

message









<asp:Label ID="defaultDescription" runat="server" Text='<%#
Eval("description") == null ? string.Empty : ((string)
Eval("description")).Substring(0, 50) %>

just to be on the safe side :p

Mythran- Hide quoted text -

- Show quoted text -

Thanks guys.. apprecaite the replies.

I ended up creating anew method, where i can do much more string
manipulation, then return the result. My ASPX page now uses <%#
getDescription() %> (there I can replace chars, truncate the length,
search for "http://" and replace with "<a href='http://'" for example.
 

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