简体   繁体   中英

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. In Asp.net

code in.aspx file

 <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

 ##  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.

Try a OnClientClick instead and make sure to return false from the function that you call there.

<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. 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.

See also: Disable the postback on an <ASP:LinkButton>

I think UpdatePnael and PostBackTrigger can help you partially update the page without full postback. Adding Update Panel surrounding your link button and adding PostBackTrigger can help in your situation. For more details see this answer

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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