tags:

views:

470

answers:

1

I want to display only 100 characters at Gridviews ItemTemplate. When I click edit button I want to display all binded value with EditItemTemplate.

<ItemTemplate>

<asp:Label ID="Label2" runat="server" Text='<%# Bind("Address") %>'> </asp:Label>

</ItemTemplate>

How can I limit label text 100 characters ?

+2  A: 

Since you are working in the ItemTemplate you don't really need to use Bind(). You can use Eval() instead and make something like:

<%# Eval("Address").ToString().Substring(0, 100) %>

There are two problems with such a simple solution:

The first is when the address field is null, you have to make a check of that. The second is if the string is shorter than 100 characters it will also fail since .NETs Substring() tries to ensure that you always get exactly 100 characters and throws an exception if the string is shorter. So you should add code to make sure that you really need to cut the string.

And by now it feels like maybe we should make a little helper method instead:

public static class Extensions
{
    public static String Limit(this String s, int length)
    {
        if (s == null)
            return String.Empty;

        return s.Substring(0, Math.Min(s.Length, length));
    }
}

Then the Eval statement will look like:

<%# ((string)Eval("Address")).Limit(100) %>

(This assumes that Address really is a string)

Marpe
sorry.. error occured: Error 2 'object' does not contain a definition for 'SubString' and no extension method 'SubString' accepting a first argument of type 'object' could be found (are you missing a using directive or an assembly reference?)
Jack
I'm not sure what Eval returns. Try making a .ToString().Substring(0, 100) instead. And, yes, there is no capital S on String, just a typo.
Marpe
"The Eval method takes the name of a data field and returns a string containing the value of that field from the current record in the data source." - from http://msdn.microsoft.com/en-us/library/ms178366.aspx. So it should work. Maybe it was just the typo that caused your problem.
Marpe
no it couldnt. same error happened
Jack
I just tried it in a litte test project and it works for me. It fails if the field I'm trying to parse is null though.
Marpe
@Marpe: That syntax (after the edit) is accurate. The OP's error seems to originate from some other problem.
Cerebrus
yes thats the correct one.I made a little quote mistake. thanks<%# Eval("Address").ToString().Substring(0, 100) %>.
Jack
You're welcome. I expanded my answer a little after actually testing it in a little web. So now it plays nicer with nulls and short strings.
Marpe
woow that will be desert on this :) thanks for extension method
Jack
Sometimes I m getting this error message:Error 2 'string' does not contain a definition for 'Limit' and no extension method 'Limit' accepting a first argument of type 'string' could be found (are you missing a using directive or an assembly reference? 132 I put extension code only one aspx file.Do you have any idea?
Jack
Create the Extensions class on its own in a file called Extensions.cs. Then on the page you want to use the extension you add a directive at the top of the page:<%@ Import Namespace="TheNamespaceYouUsedForTheClass" %>You could also do it for all pages in Web.config using:Under the System.Web/Pages section add <namespaces><add namespace="YourNamespace" /></namespaces>
Marpe