【发布时间】:2023-03-13 00:29:01
【问题描述】:
实际上,如果数据超过 40 个字符,我使用了 read more 并隐藏两个按钮,它工作正常,但是当单击按钮时它正在刷新页面如何禁用刷新。在 Asp.net 中
.aspx 文件中的代码
<asp:TemplateField HeaderText="UserdetailsDescription" ItemStyle-Width="50">
<ItemTemplate>
<asp:Label ID="lblDescription" runat="server"
Text='<%# Limit(Eval("UserdetailsDescription"),40) %>'
Tooltip='<%# Eval("UserdetailsDescription") %>'>
</asp:Label>
<asp:LinkButton ID="ReadMoreLinkButton" runat="server"
Text="Read More"
autopostback="false"
Visible='<%# SetVisibility(Eval("UserdetailsDescription"), 40) %>'
OnClick="ReadMoreLinkButton_Click">
</asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
.CS 文件后面的##code
## 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) + ".....";
}
【问题讨论】:
-
一个 LinkButton 完全按照它所说的那样做,它是一个按钮,当点击时,它使浏览器导航到一个新链接(刷新)。如果您不希望这种情况发生,则需要使用常规按钮并使用 JavaScript 实现它。
标签: c# asp.net asp.net-mvc asp.net-core