0

DataList 内のラベルのテキストを切り取ろうとしています。

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

説明の長さが 100 文字を超える場合は、説明がトリミングされ、「もっと見る」リンクが表示されることを望みます (「少なく表示する」は必要ありません)。「もっと見る」をクリックすると、完全な説明が同じページに表示されます (ここで jQuery を使用したいと思います)。

この問題の解決策を始めるのを手伝ってくれる人はいますか?

4

1 に答える 1

0

これは古い質問であることは知っていますが、回答のために:

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% 賛成しているわけではありません。

于 2014-11-03T04:10:09.387 に答える