gpt4 book ai didi

c# - 如何使用阅读更多链接限制 GridView 中的标签字符串长度?

转载 作者:行者123 更新时间:2023-11-30 20:09:36 26 4
gpt4 key购买 nike

目前我是这样用的...

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

但我不知道如何放置 [Read More] 链接...

最佳答案

做这样的事情。

标记

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

关于c# - 如何使用阅读更多链接限制 GridView 中的标签字符串长度?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6012942/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com