asp.net linkbutton in updatepanel doesn't fire

送分小仙女□ 提交于 2019-12-01 17:12:44

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);
        }
    }
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!