1

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) + ".....";
    }
  • A LinkButton does exactly what it says, it's a button that makes the browser navigate to a new link (refresh) when clicked. If you don't want that to happen, you need to use a regular button and implement it using JavaScript. – Rudey Aug 14 '20 at 09:44

2 Answers2

0

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>

Jan Schultke
  • 17,446
  • 6
  • 47
  • 96
Salik Rafiq
  • 174
  • 1
  • 7
0

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

Nagib Mahfuz
  • 833
  • 10
  • 19