Not sure how UpdatePanel works for WebForms
Hi all,
I asked this question on SO twice (deleted the original) but I can never seem to get a response. If anyone here knows a lot about WebForms, and especially UpdatePanel behaviour, then I would love to hear from you. This sample code was quickly put together in a fresh WebForms project and can be copied and pasted to see the strange problem I am having. Thanks for reading!
I have a basic WebForms page using the default master page generated by a new WebForms project. This page contains an UpdatePanel:
<asp:UpdatePanel ID="Content" runat="server" UpdateMode="Conditional" ChildrenAsTriggers="False">
As you can see, UpdateModel is set to Conditional and ChildrenAsTriggers is set to false. From my understanding, only controls marked as AsyncPostBackTrigger should cause an async postback using this setup, such as the 1 Button used in the included example:
<Triggers> <asp:AsyncPostBackTrigger ControlID="btnLoadContent" EventName="Command" /> </Triggers>
Inside this UpdatePanel is a custom User Control. The User Control has a GridView and some other arbitrary controls containing links. Some of these controls have event handlers, such as click events, in the "code behind".
The problem is that every single link in the user control triggers the parent page's UpdatePanel. The effect of this is that every event handler is being ignored and the only breakpoint hit is the main page's Page_Load function. If I delete the UpdatePanel then everything works as expected.
I do not want the UpdatePanel to affect the UserControl contained within it. I only want 1 Button to trigger an async request (which loads the user control programmatically and injects it into a <asp:PlaceHolder .../> element, hence this is why UserControl must be placed in the UpdatePanel).
I have tried altering the UpdateMode and ChildrenAsTriggers to every permutation possible and still no luck. Is there something I am not understanding?
I created a fresh WebForms project using Visual Basic (version number does not matter as I have tried 4.5.2 and 4.7.1 and a few other versions with the same effect).
Default.aspx:
<%@ Page Title="Home Page" Language="VB" MasterPageFile="~/Site.Master" AutoEventWireup="true" CodeBehind="Default.aspx.vb" Inherits="UpdatePanelTest._Default" %> <asp:Content ID="BodyContent" ContentPlaceHolderID="MainContent" runat="server"> <asp:UpdatePanel ID="upLoadContent" runat="server" UpdateMode="Conditional" ChildrenAsTriggers="False"> <Triggers> <asp:AsyncPostBackTrigger ControlID="btnLoadContent" EventName="Command" /> </Triggers> <ContentTemplate> <asp:LinkButton id="btnLoadContent" Runat="server" Text="Load Content" OnCommand="LoadContent_OnCommand" /> <asp:PlaceHolder runat="server" ID="phContentArea" /> </ContentTemplate> </asp:UpdatePanel> </asp:Content>
Default.aspx.vb:
Friend Interface INestedView Sub Show() End Interface Public Class _Default Inherits Page Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load If Not Page.IsPostBack Then Console.WriteLine("MainUpdatePanelPage_Page_Load") btnLoadContent.DataBind() End If End Sub 'Load MyUserControl programmatically and add into Placeholder Public Sub LoadContent_OnCommand(sender As Object, e As EventArgs) Dim loadedContent As UserControl Dim myUserControl As INestedView loadedContent = LoadControl("MyUserControl.ascx") loadedContent.Visible = True myUserControl = CType(loadedContent, INestedView) myUserControl.Show() phContentArea.Controls.Clear() phContentArea.Controls.Add(loadedContent) End Sub End Class
MyUserControl.ascx:
<%@ Control Language="vb" AutoEventWireup="false" CodeBehind="MyUserControl.ascx.vb" Inherits="UpdatePanelTest.MyUserControl" %> <asp:Label runat="server" ID="lblTotalResults" Text="Total Results Found: 0" /> <asp:GridView runat="server" ID="gvResults" CssClass="dataGrid" AutoGenerateColumns="False" ShowHeaderWhenEmpty="True" OnRowCommand="gvResults_RowCommand" ItemType="UpdatePanelTest.Customer"> <HeaderStyle CssClass="tableHeader" /> <Columns> <%-- Customer Name --%> <asp:TemplateField HeaderStyle-Width="90px" HeaderText="Customer Name"> <ItemTemplate> <asp:LinkButton runat="server" ID="lnkCustomerName" Text="<%# Item.CustomerName %>" CommandName="ViewCustomer" CommandArgument="<%# Item.CustomerID %>" /> </ItemTemplate> </asp:TemplateField> <%-- Next Task Name --%> <asp:TemplateField HeaderStyle-Width="160px" HeaderText="Next Task"> <ItemTemplate> <asp:LinkButton runat="server" ID="lnkTaskName" Text="<%# Item.NextTaskName %>" CommandName="ViewTask" CommandArgument="<%# Item.NextTaskID %>" /> </ItemTemplate> </asp:TemplateField> <%-- Customer Contact Button --%> <asp:TemplateField HeaderStyle-Width="90px" HeaderText="Contact"> <ItemTemplate> <asp:Button runat="server" ID="btnContactCustomer" Text="Contact" OnClick="btnContactCustomer_Click"/> </ItemTemplate> </asp:TemplateField> </Columns> </asp:GridView>
MyUserControl.ascx.vb:
Option Strict On Option Explicit On Public Class MyUserControl Inherits UserControl Implements INestedView Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load Console.WriteLine("MyUserControl_Page_Load") End Sub Public Sub Show() Implements INestedView.Show Dim customers As New List(Of Customer) From { New Customer With {.CustomerID = 1, .CustomerName = "John", .NextTaskID = 1, .NextTaskName = "Initial Call"}, New Customer With {.CustomerID = 2, .CustomerName = "Mike", .NextTaskID = 2, .NextTaskName = "Follow Up Call"}, New Customer With {.CustomerID = 3, .CustomerName = "Jake", .NextTaskID = 3, .NextTaskName = "Request Docs"}, New Customer With {.CustomerID = 4, .CustomerName = "Sarah", .NextTaskID = 4, .NextTaskName = "Process eSign"}, New Customer With {.CustomerID = 5, .CustomerName = "Jill", .NextTaskID = 5, .NextTaskName = "Call Customer"}, New Customer With {.CustomerID = 6, .CustomerName = "A’dab", .NextTaskID = 6, .NextTaskName = "Request Docs"}, New Customer With {.CustomerID = 7, .CustomerName = "Zoya", .NextTaskID = 7, .NextTaskName = "Call Customer"} } lblTotalResults.Text = $"Total Results Found: {customers.Count}" gvResults.DataSource = customers gvResults.DataBind() End Sub 'BUG: Is being ignored! Protected Sub gvResults_RowCommand(sender As Object, e As GridViewCommandEventArgs) Console.Write("gvResults_RowCommand") End Sub 'BUG: Is being ignored! Protected Sub btnContactCustomer_Click(sender As Object, e As EventArgs) Console.Write("btnContactCustomer_Click") End Sub End Class
0 comments:
Post a Comment