asp.netupdatepanellinkbutton

Disable LinkButton in UpdatePanel after first click


I am working on an ASP.NET webforms page that has the following asp markup (with additional controls stripped out):

<asp:UpdatePanel ID="updatePanel1" runat="server">
    <ContentTemplate>
        <asp:LinkButton ID="lnkbtnPreviousTop" OnClick="LinkButtonPrevious_Click"
            Text="Previous" runat="server">
        </asp:LinkButton>
    </ContentTemplate>
</asp:UpdatePanel>

Right now, if you click this LinkButton multiple times, the LinkButtonPrevious_Click event handler on the server side will fire as many times as the link was clicked. How can I make it so that the lnkbtnPreviousTop LinkButton is disabled after the first click, but the event handler still fires once and the UpdatePanel is refreshed?

I have tried adding this.disabled = true; to the OnClick attribute, but then the event handler code never gets hit.


Solution

  • Just call the postback directly in the onclick, something like:

    lnkbtnPreviousTop.Attributes.Add("onclick", "this.disabled=true;" + Page.ClientScript.GetPostBackEventReference(lnkbtnPreviousTop, "").ToString());