trek
trek

Reputation: 49

Disable Postback for Linkbutton

Im using ASP.NET LinkButton. I have a LinkButton which calls code behind from onclick:

on aspx:

 <asp:LinkButton ID="LinkTest" runat="server" onclick="LinkTest_Click" OnClientClick="return false;"  >LinkButton</asp:LinkButton>

on aspx.cs:

 protected void LinkTest_Click(object sender, EventArgs e)
    {
        //code here
    }

As you can see I have set OnClientClick="return false;" so that it wont do a postback. The postback does not happens. But the code behind is not fired. I want the code behind to fire and I want no postback when the linkbutton is clicked.

How can I do this?

Thanks.

Upvotes: 1

Views: 12098

Answers (2)

mshsayem
mshsayem

Reputation: 18038

Remove the OnclientClick attribute of the linkbutton. Try using an UpdatePanel like:

<asp:UpdatePanel ID="up1" runat="server" UpdateMode="Conditional">
    <Triggers>
        <asp:AsyncPostBackTrigger ControlID="LinkTest" EventName="Click" />
    </Triggers>
    <ContentTemplate>
          <asp:label id="Label1" runat="server" Text="Initial Label" />
    </ContentTemplate>
</asp:UpdatePanel>

Also, you will need to add a ScriptManager in your page.

Upvotes: 2

Nitin S
Nitin S

Reputation: 7598

Add the linkbutton inside updatepanel, remove onclientclick attribute of the linkbutton:

Page:

    <asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager>
    <asp:UpdatePanel ID="UpdatePanel1" runat="server">
        <ContentTemplate>
            <asp:Label ID="lblName" runat="server" Text="Old Value"></asp:Label>
            <br />
            <asp:LinkButton ID="lnkbtChange" runat="server" OnClick="lnkbtChange_Click">Change</asp:LinkButton>
        </ContentTemplate>
    </asp:UpdatePanel>

CodeBehind:

protected void lnkbtChange_Click(object sender, EventArgs e)
{
    lblName.Text = "New value";
}

Upvotes: 1

Related Questions