2011-05-16 97 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> 
相關問題