User Control Exception in Page Edit Mode
I have encountered an exception in page edit mode with user controls that refer to contained controls in the OnInit() method. To illustrate the issue, I create a simple user control that contains only the following single standard control in the ASCX file:
<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="BuggyControl.ascx.cs" Inherits="SitefinityWebApp.UserControls.Misc.BuggyControl" %>
<asp:Button ID="Button1" runat="server" Text="Button" />
The code behind file attaches an event handler within the OnInit() method following this familiar pattern:
namespace SitefinityWebApp.UserControls.Misc
public partial class BuggyControl : System.Web.UI.UserControl
protected void Page_Load(object sender, EventArgs e)
protected override void OnInit(EventArgs e)
InitializeComponent();
base.OnInit(e);
private void InitializeComponent()
Button1.Click += new EventHandler(Button1_Click);
void Button1_Click(object sender, EventArgs e)
// do nothing
The single asp:Button object is declared in the designer.cs file as usual:
public partial class BuggyControl
protected global::System.Web.UI.WebControls.Button Button1;
When I drop this control onto a page in CMS page Edit mode, it does not render. Instead, the following appears:
Exception of type 'System.Web.HttpUnhandledException' was thrown.
When I set a break point in the InitializeComponent() method on this statement:
Button1.Click += new EventHandler(Button1_Click);
I can verify that the problem is caused because Button1 is null, so a Null Reference exception occurs.
This issue does not occur when I Preview the page - I can see that Button1 is properly initialized and contains a reference to the asp:Button control.
I realize that one solution that would resolve this issue is to add an 'onclick="Button1_Click"' attribute to asp:Button markup and remove the reference in the OnInit() handler, but I have a large number of user controls that work in Sitefinity 3.7 and I am trying to port them to 4.0 (currently using 4.0.1030.0).
I don't want to have to rewrite them all.
Hi Rick,
Inside OnInit the server controls cannot be accessed. The button1 is null and this is why get an error.
Greetings,
Ivan Dimitrov
the Telerik team
Hi Ivan - I know the server control can't be referenced in OnInit because it is null. I pointed that out in my initial post.
The point is, I think this is a bug because Sitefiniity 3.7 does not have this issue, and referencing server controls in the OnInit() method is a typical pattern when implementing a user control.
Hi Rick,
According to MSDN OnInit cannot guaranteed that all controls will be created and ready for access.
Best wishes,
Ivan Dimitrov
the Telerik team
Hi Ivan:
I did find a reference on a draft .NET 4.0 MSDN page stating the fact that server controls are not guaranteed to be initialized in OnInit, however this seems to contradict another MSDN page here:
http://msdn.microsoft.com/en-us/library/ms178472.aspx
Which contains the following information regarding the Init event:
Raised after all controls have been initialized and any skin settings have been applied. The Init event of individual controls occurs before the Init event of the page. Use this event to read or initialize control properties. |