asp.net linkbutton in updatepanel doesn’t fire

I have a asp.net web application. In my .aspx page I have a update panel in which I have 3 asp:LinkButton that should make a call to a c# code behind. The problem is that the onclick doesn’t work.

Here is how the code looks:

<div id="div1">
                <asp:UpdatePanel ID="UpdatePanel2" runat="server">
                    <ContentTemplate>
                        <ul>
                            <li><asp:LinkButton ID="lnk_1" runat="server" OnClick="lnk1_Click">Link1</asp:LinkButton></li>
                            <li><asp:LinkButton ID="lnk_2" runat="server" OnClick="lnk2_Click">Link2</asp:LinkButton></li>
                            <li><asp:LinkButton ID="lnk_3" runat="server" OnClick="lnk3_Click">Link3</asp:LinkButton></li>
                        </ul> 
<div> some more code here </div>
</ContentTemplate>
                    <Triggers>
                        <asp:PostBackTrigger ControlID="lnk_1" />
                        <asp:PostBackTrigger ControlID="lnk_2" />
                        <asp:PostBackTrigger ControlID="lnk_3" />
                    </Triggers>
                </asp:UpdatePanel>
            </div>

What is wrong with the code? I have also tried using AsyncPostBackTrigger but still doesn’t work.

The code behind is not invoked at all.

I have also tried to search on Google but couldn’t find a solution.

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

You’re very close. Couple of things:

  • Your triggers should be AsyncPostBackTriggers as you said you tried.
  • Your triggers need an event name.
  • Suggestion: This won’t prevent your events from firing, but unless you want EVERY postable event to cause a postback, add UpdateMode=”Conditional” to your UpdatePanel.

Here is a working example.

Web Form – WebForm1.aspx:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="AspDotNetStorefront.WebForm1" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title></title>
    </head>
    <body>
        <form id="form1" runat="server">
            <asp:ScriptManager runat="server"></asp:ScriptManager>
            <div id="div1">
                <asp:UpdatePanel ID="UpdatePanel2" runat="server" UpdateMode="Conditional">
                    <ContentTemplate>
                        <ul>
                            <li><asp:LinkButton ID="lnk_1" runat="server" OnClick="lnk1_Click">Never clicked</asp:LinkButton></li>
                        </ul> 
                    </ContentTemplate>
                    <Triggers>
                        <asp:AsyncPostBackTrigger ControlID="lnk_1" EventName="Click" />
                    </Triggers>
                </asp:UpdatePanel>
            </div>    
        </form>
    </body>
</html>

CodeBehind – WebForm1.aspx.cs:

using System;

namespace AspDotNetStorefront
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        private static int _clickedCount = 0;

        protected void lnk1_Click(object sender, EventArgs e)
        {
            ++_clickedCount;
            var suffix = _clickedCount <= 1 ? "time" : "times";
            lnk_1.Text = string.Format("Clicked {0} {1}", _clickedCount, suffix);
        }
    }
}


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x