Wednesday, March 28, 2012

Trmming a Field in .NET

In Classic ASP I can take a description field and only display the first 260 characters like this: <%= Left((rsFeature.Fields.Item("description").Value), 260) %>...

Any ideas how I would accomplish this in .NET?

Thanks for any help.

try

<%= rsFeature.Fields.Item("description").Value.ToString().Substring(0,260) %>

substring will split up a string given two parameters, start index, and length.

hth,

mcm


You don′t have Left() and Right() in .NET, but you can use Substring.

string s ="Hello, world!";Console.WriteLine(s.SubString(0, 5));//prints "Hello"

So I tried this: <%# dsFeature.FieldValue("description", Container).ToString().Substring(0,260) %>...

and get this error:

System.ArgumentOutOfRangeException: Index and length must refer to a location within the string.Parameter name: length


What if the description string does not contain 260 characters?


<%# dsFeature.FieldValue("description", Container).ToString().Substring(0,100) %>... works great.

However, if I use <%# dsFeature.FieldValue("description", Container).ToString().Substring(0,260) %>... I get an error because some of my descriptions have less than 260 characters. Any idea how I can handle this?

Thanks!


what you should do is use the code-behind to evaluate your string expression

so

string tmpStr = "";
if(theString.length > 260){
tmpStr = theString.Substring(0,260);
}else{
tmpStr = thisString.ToString();
}

and just bind tmpStr to your field in the front end.

hth,
mcm


Got it! Thanks!

0 comments:

Post a Comment