User Control Exception in Page Edit Mode

Posted by Community Admin on 04-Aug-2018 20:26

User Control Exception in Page Edit Mode

All Replies

Posted by Community Admin on 09-Jan-2011 00:00

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.







Posted by Community Admin on 10-Jan-2011 00:00

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

Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items

Posted by Community Admin on 10-Jan-2011 00:00

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.

Posted by Community Admin on 10-Jan-2011 00:00

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

Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items

Posted by Community Admin on 11-Jan-2011 00:00

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:

Init

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.


In VS 2003, Microsoft always added code to the OnInit method to wire up event handlers. Although they stopped doing that in VS 2005, I've never had an issue inside the OnInit method referencing server controls declared in my code behind file. The references have always been initialized by the framework (this is true up to and including ASP.NET 4.0). This is also the case with Siftefinity 3.7. In Sitefinty 4,0, the server control references are always initialized when I view the page in Preview or Live mode, but is not the case in Edit mode.


This thread is closed