2011-05-16 98 views
2

目前我使用的是这样的...如何使用Read More链接限制GridView中的标签字符串长度?

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

但我不知道如何把[阅读全文]链接...

回答

7

做这样的事情。

标记

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

我想为你的答案投票...但没有足够的声望。 :( 现在这一个是阅读更多,仪式?如何隐藏。谢谢 – CMMaung 2011-05-16 05:56:42

+0

现在可以投票:) – CMMaung 2011-05-16 05:59:37

+1

升级代码。希望这可以帮助。 – naveen 2011-05-16 06:04:05

0

在标签后添加一个不可见的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) + "..."; 
} 
+0

谢谢您建议......你的答案可能有帮助,但我不'没有链接的ReadMorePage。 – CMMaung 2011-05-16 05:57:27

+0

所以你希望它保持在同一页面上,但如果点击它们就展开?将“锚点”作为一个按钮(或链接按钮,如果你仍然想要[Read More]'看起来),并在这种情况下限制返回完整的字符串,如果按钮被点击。 – 2011-05-16 09:53:11

+0

是希望当他们点击时保持在同一页...如何更改你的代码... thx为你的回复 – CMMaung 2011-05-17 04:15:57

0

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

在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> 
相关问题