【问题标题】:How to limit label string length in GridView with Read More link?如何使用 Read More 链接限制 GridView 中的标签字符串长度?
【发布时间】:2011-05-16 03:33:30
【问题描述】:

目前我是这样用的……

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

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

【问题讨论】:

    标签: c# asp.net string gridview


    【解决方案1】:

    做这样的事情。

    标记

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

    【讨论】:

    • 我想为你的答案投票……但声望不够。 :( 现在这个是阅读更多,仪式?隐藏怎么样。谢谢
    • 当前上下文中不存在名称“Limit”?
    • @Azzy:从问题中获取限制函数。将函数签名从 public static string Limit(object Desc, int length) 更改为 protected string Limit(object Desc, int length)
    【解决方案2】:

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

    <asp:TemplateField HeaderText="Description">
     <ItemTemplate>
          <asp:Label ID="lblDescription" runat="server"
                    Text='<%# Limit(Eval("Description"),40) %>' >
          </asp:Label>
          <a href="TheReadMorePage" ID="aReadMore" runat="server" Visible="false">[Read More]</a>
     </ItemTemplate>
    
    
    if(strDesc.Length > length)
    {
        var anchor = ((Label)Desc).NamingContainer.FindControl("aReadMore");
        anchor.Visible = true;
        return strDesc.ToString().Substring(0, length) + "...";
    }
    

    【讨论】:

    • 感谢您的建议...您的回答可能会有所帮助,但我没有阅读更多页面的链接。
    • 所以你希望它保持在同一页面上,但如果他们点击就展开?将“锚点”设为按钮(或链接按钮,如果您仍然想要 [Read More] 外观),如果单击按钮,则在这种情况下让 Limit 返回完整的字符串。
    • 是的,当他们点击时希望保持在同一页面...如何更改您的代码...谢谢您的回复
    【解决方案3】:

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

    在 ASP.Net 页面中:

    <script type="text/javascript" src="http://code.jquery.com/jquery-1.8.2.js"></script>
    <script type="text/javascript" src="http://code.jquery.com/ui/1.10.3/jquery-ui.js"></script>
    <script type="text/javascript">
    $(function() {
    $('#btnReadMore').click(function() {
    $("#popupdiv").dialog({
    title: "jQuery Popup from Server Side",
    width: 430,
    height: 250,
    modal: true,
    buttons: {
    Close: function() {
    $(this).dialog('close');
    }
    }
    });
    });
    })
    
    
    <asp:TemplateField HeaderText="Description">
     <ItemTemplate>
          <asp:Label ID="lblDescription" runat="server"
                    Text='<%# Limit(Eval("Description"),40) %>' 
                    Tooltip='<%# Eval("Description") %>'>
          </asp:Label>
          <input type="button" id="btnReadMore" value="Show Modal Popup" />
    
     </ItemTemplate>
    </asp:TemplateField>
    
    <div>
    <div id="popupdiv" title="Basic modal dialog" style="display: none">
    </div>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-11-21
      • 2015-07-11
      • 1970-01-01
      • 2017-11-12
      • 1970-01-01
      相关资源
      最近更新 更多