如何使用 Read More 链接限制 GridView 中的标签字符串长度? [英] How to limit label string length in GridView with Read More link?

查看:19
本文介绍了如何使用 Read More 链接限制 GridView 中的标签字符串长度?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目前我是这样使用的...

Currently I used like this...

<asp:TemplateField HeaderText="Description">
 <ItemTemplate>
      <asp:Label ID="lblDescription" runat="server"
                Text='<%# Limit(Eval("Description"),40) %>' >
      </asp:Label>
 </ItemTemplate>

辅助函数:

public static string Limit(object Desc, int length)
{
    StringBuilder strDesc = new StringBuilder();
    strDesc.Insert(0, Desc.ToString());

    if (strDesc.Length > length)
        return strDesc.ToString().Substring(0, length) + "..." + [Read More];
    else return strDesc.ToString();
}

但我不知道如何放置[阅读更多]链接...

But I have no idea how to put [Read More] link...

推荐答案

做这样的事情.

标记

<asp:TemplateField HeaderText="Description">
 <ItemTemplate>
      <asp:Label ID="lblDescription" runat="server"
                Text='<%# Limit(Eval("Description"),40) %>' 
                Tooltip='<%# Eval("Description") %>'>
      </asp:Label>
      <asp:LinkButton ID="ReadMoreLinkButton" runat="server"
                Text="Read More"
                Visible='<%# SetVisibility(Eval("Description"), 40) %>'
                OnClick="ReadMoreLinkButton_Click">
      </asp:LinkButton>
 </ItemTemplate>
</asp:TemplateField>

和代码隐藏

protected bool SetVisibility(object desc, int maxLength)
{
    var description = (string)desc;
    if (string.IsNullOrEmpty(description)) { return false; }
    return description.Length > maxLength;
}

protected void ReadMoreLinkButton_Click(object sender, EventArgs e)
{
    LinkButton button = (LinkButton)sender;
    GridViewRow row = button.NamingContainer as GridViewRow;
    Label descLabel = row.FindControl("lblDescription") as Label;
    button.Text = (button.Text == "Read More") ? "Hide" : "Read More";
    string temp = descLabel.Text;
    descLabel.Text = descLabel.ToolTip;
    descLabel.ToolTip = temp;
}

protected string Limit(object desc, int maxLength)
{
    var description = (string)desc;
    if (string.IsNullOrEmpty(description)) { return description; }
    return description.Length <= maxLength ? 
        description : description.Substring(0, maxLength)+ "...";
}

这篇关于如何使用 Read More 链接限制 GridView 中的标签字符串长度?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆