これは古い質問であることは知っていますが、回答のために:
public static class Extensions
{
/// <summary>
/// Truncate a value if it is a string.
/// </summary>
/// <param name="value">The value to check and truncate.</param>
/// <param name="length">The length to truncate to.</param>
/// <param name="appendEllipses">Append ellipses to the end of the value?</param>
/// <returns>A truncated string, if not a string the object value.</returns>
public static object Truncate(this object value, int length, bool appendEllipses = true)
{
// Are we dealing with a string value?
var result = value as string;
// Yes? truncate, otherwise pass through.
return result != null ? Truncate(result, length, appendEllipses) : value;
}
/// <summary>
/// Truncate a value if it is a string.
/// </summary>
/// <param name="value">The value to check and truncate.</param>
/// <param name="length">The length to truncate to.</param>
/// <param name="appendEllipses">Append elipses to the end of the value?</param>
/// <returns></returns>
public static string Truncate(this string value, int length, bool appendEllipses = true)
{
var result = value;
// Too Long?
if (value.Length > length)
{
// Truncate.
result = value.Substring(0, length);
// Add Ellipses, if needed.
if (appendEllipses) { result = result + " ..."; }
}
return result;
}
}
次のように2番目の方法を使用できます。
<%# Eval("Description").ToString().Truncate(10) %>
または、混乱を避けるために、最初の方法を次のように使用できます。
<%# Eval("Description").Truncate(10) %>
オブジェクト クラスに拡張メソッドを追加することに 100% 賛成しているわけではありません。