简体   繁体   English

如何避免 asp.net 中的页面刷新同时单击链接按钮整个页面正在刷新

[英]How to avoid page refresh in asp.net while click on link button the entire page is refreshing

Actually I used read more and hide two button for if data More than 40 character,it's working fine but it's refreshing the page when click on button how to disable the refresh.实际上,如果数据超过 40 个字符,我使用了 read more 并隐藏了两个按钮,它工作正常,但是当单击按钮时它正在刷新页面如何禁用刷新。 In Asp.net在 Asp.net

code in.aspx file代码 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 Behind.CS file ##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) + ".....";
    }

There is no autopostback attribute on a linkbutton as far as I know.据我所知,链接按钮上没有autopostback属性。

Try a OnClientClick instead and make sure to return false from the function that you call there.尝试使用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>

Note: if you get a page postback by another button the button will go back to visible because that is that state it is in kept in viewstate.注意:如果您通过另一个按钮获得页面回发,则该按钮将 go 恢复可见,因为那是 state 它保持在视图状态。 So an option there is to have a hidden field maintain it's client state and a bit of JS on the page to restore the links back to hidden.因此,一个选项是有一个隐藏字段维护它的客户端 state 和页面上的一些 JS 以将链接恢复为隐藏状态。

See also: Disable the postback on an <ASP:LinkButton>另请参阅: 禁用 <ASP:LinkButton> 上的回发

I think UpdatePnael and PostBackTrigger can help you partially update the page without full postback.我认为UpdatePnaelPostBackTrigger可以帮助您部分更新页面而无需完全回发。 Adding Update Panel surrounding your link button and adding PostBackTrigger can help in your situation.在链接按钮周围添加更新面板并添加PostBackTrigger可以帮助您解决问题。 For more details see this answer有关更多详细信息, 请参阅此答案

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM