如何解决如何在单击链接按钮的同时避免整个页面在ASP.NET中刷新
实际上,我使用了阅读更多信息并隐藏了两个按钮,以查看数据是否超过40个字符,它可以正常工作,但是当单击按钮如何禁用刷新时它正在刷新页面。在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) + ".....";
}
解决方法
据我所知,链接按钮上没有autopostback
属性。
请改用OnClientClick
,并确保从您在那里调用的函数返回false。
<asp:LinkButton ID="ReadMoreLinkButton" runat="server"
Text="Read More"
Visible='<%# SetVisibility(Eval("UserdetailsDescription"),40) %>'
OnClientClick="HideReadMoreLinkButton(); return false"/>
<script>
function HideReadMoreLinkButton() {
//your code to hide button here
}
</script>
注意:如果您通过另一个按钮获得页面回发,则该按钮将返回到可见状态,因为那是它处于查看状态的状态。因此,可以选择一个隐藏字段来维护其客户端状态,并在页面上添加一些JS以将链接恢复回隐藏状态。
另请参阅:Disable the postback on an <ASP:LinkButton>
,我认为PostBackTrigger
和PostBackTrigger
可以帮助您部分更新页面而无需完全回发。在链接按钮周围添加更新面板并添加{{1}}可以在您遇到的情况中提供帮助。有关更多详细信息,see this answer
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。