[英]How to avoid page refresh in asp.net while click on link button the entire page is refreshing
实际上,如果数据超过 40 个字符,我使用了 read more 并隐藏了两个按钮,它工作正常,但是当单击按钮时它正在刷新页面如何禁用刷新。 在 Asp.net
代码 in.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>
##code 后面的.CS 文件
## 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
并确保从您在那里调用的 function 中返回 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>
注意:如果您通过另一个按钮获得页面回发,则该按钮将 go 恢复可见,因为那是 state 它保持在视图状态。 因此,一个选项是有一个隐藏字段维护它的客户端 state 和页面上的一些 JS 以将链接恢复为隐藏状态。
另请参阅: 禁用 <ASP:LinkButton> 上的回发
我认为UpdatePnael
和PostBackTrigger
可以帮助您部分更新页面而无需完全回发。 在链接按钮周围添加更新面板并添加PostBackTrigger
可以帮助您解决问题。 有关更多详细信息, 请参阅此答案
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.