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

前端 未结 3 564
孤城傲影
孤城傲影 2021-01-14 23:36

Currently I used like this...


 
      

        
3条回答
  •  死守一世寂寞
    2021-01-15 00:01

    Do something like this.

    Markup

    
     
          
          
          
          
     
    
    

    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)+ "...";
    }
    

提交回复
热议问题