The purpose of what I am trying to do is to create a postback-free web application through the use of ASP.net AJAX UpdatePanels and User Controls. When programmatically adding a User Control to a web page through a normal postback everything works fine. All the controls within the user control are registered properly in the page and any update panels included in the user control also work properly. HOWEVER, if instead of using a full postback you use an UpdatePanel and a partial page update of the UpdatePanel the controls do not get registered with the page and events from them do not fire (for instance, a button click event never hits the event breakpoint).
Because the very same user control works fine if loaded in a full postback or dynamically added from a namespace works fine, I can be relatively sure that it only is trouble when loading via a partial page update into an UpdatePanel. I load the control via the LoadConrol method and then add it to the page via a PlaceHolder control. Theoretically, adding the User Control to the PlaceHolder should register itself and it's controls and events with the page, but it does not.
The following code sample is a UpdatePanel-free page using a user control that works, later I will show the same code with an UpdatePanel that does not.
I think I need to figure out how to register the controls and their events with the page without going through a full page postback. Any suggestions??
This example works as expected:
Default.aspx:
Expand|Select|Wrap|Line Numbers
- <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
- <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
- <html xmlns="http://www.w3.org/1999/xhtml">
- <head runat="server">
- <title>Untitled Page</title>
- </head>
- <body>
- <form id="form1" runat="server">
- <div>
- <asp:PlaceHolder ID="UCPlaceHolder" runat="server"></asp:PlaceHolder>
- </div>
- </form>
- </body>
- </html>
Expand|Select|Wrap|Line Numbers
- using System;
- using System.Data;
- using System.Configuration;
- using System.Web;
- using System.Web.Security;
- using System.Web.UI;
- using System.Web.UI.WebControls;
- using System.Web.UI.WebControls.WebParts;
- using System.Web.UI.HtmlControls;
- public partial class _Default : System.Web.UI.Page
- {
- protected void Page_Load(object sender, EventArgs e)
- {
- Control ctl = LoadControl("~/UserControlDemo.ascx");
- ctl.ID = "UC1";
- this.UCPlaceHolder.Controls.Add(ctl);
- }
- }
Expand|Select|Wrap|Line Numbers
- <%@ Control Language="C#" AutoEventWireup="true" CodeFile="UserControlDemo.ascx.cs" Inherits="UserControlDemo" %>
- <asp:Button ID="Button1" runat="server" Text="Display from UC" OnClick="Button1_Click" /> <br />
- <br />
- <asp:Label ID="Content" runat="server" Text="Content"></asp:Label>
Expand|Select|Wrap|Line Numbers
- using System;
- using System.Data;
- using System.Configuration;
- using System.Collections;
- using System.Web;
- using System.Web.Security;
- using System.Web.UI;
- using System.Web.UI.WebControls;
- using System.Web.UI.WebControls.WebParts;
- using System.Web.UI.HtmlControls;
- public partial class UserControlDemo : System.Web.UI.UserControl
- {
- protected void Page_Load(object sender, EventArgs e)
- {
- }
- protected void Button1_Click(object sender, EventArgs e)
- {
- Content.Text = "Content Changed.";
- }
- }
Default.aspx:
Expand|Select|Wrap|Line Numbers
- <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
- <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
- <html xmlns="http://www.w3.org/1999/xhtml">
- <head runat="server">
- <title>Untitled Page</title>
- </head>
- <body>
- <form id="form1" runat="server">
- <div>
- <asp:Button ID="Button1" runat="server" Text="Load User Control" OnClick="Button1_Click" />
- <br />
- <br />
- <asp:PlaceHolder ID="UCPlaceHolder" runat="server"></asp:PlaceHolder>
- </div>
- </form>
- </body>
- </html>
Expand|Select|Wrap|Line Numbers
- using System;
- using System.Data;
- using System.Configuration;
- using System.Web;
- using System.Web.Security;
- using System.Web.UI;
- using System.Web.UI.WebControls;
- using System.Web.UI.WebControls.WebParts;
- using System.Web.UI.HtmlControls;
- public partial class _Default : System.Web.UI.Page
- {
- protected void Page_Load(object sender, EventArgs e)
- {
- }
- protected void Button1_Click(object sender, EventArgs e)
- {
- Control ctl = LoadControl("~/UserControlDemo.ascx");
- ctl.ID = "UC1";
- this.UCPlaceHolder.Controls.Add(ctl);
- }
- }
Default2.aspx:
Expand|Select|Wrap|Line Numbers
- <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="Default2" %>
- <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
- <html xmlns="http://www.w3.org/1999/xhtml" >
- <head runat="server">
- <title>Untitled Page</title>
- </head>
- <body>
- <form id="form1" runat="server">
- <asp:ScriptManager ID="ScriptManager1" runat="server" EnablePartialRendering="true"></asp:ScriptManager>
- <div>
- <asp:UpdatePanel ID="UpdatePanel2" runat="server">
- <ContentTemplate>
- <asp:Button ID="Button1" runat="server" Text="Load User Control into UpdatePanel" OnClick="Button1_Click" />
- </ContentTemplate>
- </asp:UpdatePanel>
- <br />
- <asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional">
- <ContentTemplate>
- <asp:PlaceHolder ID="ContentPlaceHolder" runat="server"></asp:PlaceHolder>
- </ContentTemplate>
- <Triggers>
- <asp:AsyncPostBackTrigger ControlID="Button1" />
- </Triggers>
- </asp:UpdatePanel>
- </div>
- </form>
- </body>
- </html>
Expand|Select|Wrap|Line Numbers
- using System;
- using System.Data;
- using System.Configuration;
- using System.Collections;
- using System.Web;
- using System.Web.Security;
- using System.Web.UI;
- using System.Web.UI.WebControls;
- using System.Web.UI.WebControls.WebParts;
- using System.Web.UI.HtmlControls;
- public partial class Default2 : System.Web.UI.Page
- {
- protected void Page_Load(object sender, EventArgs e)
- {
- }
- protected void Button1_Click(object sender, EventArgs e)
- {
- Control ctl = LoadControl("~/UserControlDemo.ascx"); //loads into the page
- ctl.ID = "UC1";
- this.ContentPlaceHolder.Controls.Add(ctl); //adds to page control tree (or at least it should)
- }
- }