[英]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.