How to limit label string length in GridView with Read More link?

14,981

Do something like this.

Markup

<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>

And code-behind

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)+ "...";
}
Share:
14,981
CMMaung
Author by

CMMaung

Hi! I'm Dot net developer from Myanmar (aka) Burma. Currently resided in Singapore. I involved in ASP.NET, ASP.NET MVC, C#, AJAX, JQUERY, MSSQL, etc... Sometimes I take photo, write poem.

Updated on July 22, 2022

Comments

  • CMMaung
    CMMaung almost 2 years

    Currently I used like this...

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

    The helper function:

    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...

  • CMMaung
    CMMaung almost 13 years
    I want to do vote up for your answer... but not enough reputation. :( Now this one is Read More, rite? How about Hide. Thanks
  • CMMaung
    CMMaung almost 13 years
    Thanks for your suggestion... your answer may be helpful but I don't have link for theReadMorePage.
  • Chuck Savage
    Chuck Savage almost 13 years
    So you want it to remain on the same page but just expand if they click? Make the 'anchor' a button (or link button if you still want the [Read More] look) instead and have Limit in that case return the full string if the button is clicked.
  • CMMaung
    CMMaung almost 13 years
    yes want to remain in same page when they click... how to change ur code... thx for ur reply
  • Azzy
    Azzy about 11 years
    The name 'Limit' does not exist in the current context ?
  • naveen
    naveen about 11 years
    @Azzy: take the limit function from the question. change the function signature from public static string Limit(object Desc, int length) to protected string Limit(object Desc, int length)