如何使用“阅读更多”链接限制 GridView 中的标签字符串长度
本文关键字:GridView 标签 字符串 链接 何使用 阅读更多 | 更新日期: 2023-09-27 17:56:26
目前我是这样使用的...
<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();
}
但我不知道如何放置[阅读更多]链接...
做这样的事情。
标记
<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)+ "...";
}
在 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) + "...";
}
可以使用"模式弹出"对话框:
在 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>