如何使用“读取更多”链接限制GridView中的标签字符串长度?

目前我这样使用……

  <asp:Label ID="lblDescription" runat="server" Text='' >   

辅助函数:

 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(); } 

但我不知道如何把[阅读更多]链接…

做这样的事情。

标记

         

代码隐藏

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

在Label之后添加一个不可见的HtmlAnchor控件。 试试像,

     [Read More]  if(strDesc.Length > length) { var anchor = ((Label)Desc).NamingContainer.FindControl("aReadMore"); anchor.Visible = true; return strDesc.ToString().Substring(0, length) + "..."; } 

您可以使用“模态弹出”对话框:

在ASP.Net页面中: