Controls State: "Error Creating Control" in the Design View in Visual Studio 2010

Controls State: "Error Creating Control" in the Design View in Visual Studio 2010

Rate This
  • Comments 33

In VS 2010, if you are accessing the Session state in the OnInit(…) method of your page, similar to the following code snippet:


then you will encounter an “Error Creating Control” for ASP.NET controls when viewing the Design view of the page. For example, if you have an ASP.NET button control on the page, the designer will display the error:

Error Creating Control – Button1
Session state can only be used when enableSessionState is set to true, either in a configuration file or in the Page directive. Please also make sure that System. Web.SessionStateModule or a custom session state module is included in the <configuration>\<system.web>\<httpModules> section in the application configuration.”.

Screen shot of the error:


We are seeing this error because at the design time, some objects such as Session, are not available, or being null. However, the project still runs perfectly at runtime.

To workaround the issue, you can add code to check for the existence of the Session object as shown below, then the design view will render correctly.


We are considering adding a fix for this issue during the VS 2010 SP1 timeframe.

There are some confusion from the readers about the post, so I’m updating the blog to clarify the issue and the workarounds.

1. Steps to reproduce the issue for a website

  • Add a file Class1.cs under App_Code directory.
  • Replace the content of the file with the following code:
    namespace ClassLibrary1
        public class MyCustomPage : System.Web.UI.Page
            protected override void OnInit(EventArgs e)
               int count = Session.Count;
  • In a webform with no code behind, replace the Page directive with the following line:
    <%@ Page Language="C#" Inherits="ClassLibrary1.MyCustomPage" %>
  • Add an ASP.NET button, then switch to the design view.

2. Steps to reproduce the issue for a Web Application Project (WAP)

  • In the code behind file, WebForm1.asp.cs, add the following code snippet
    protected override void OnInit(EventArgs e)
        int count = Session.Count;
  • Save the file and compile the project.
  • Close and re-open the file WebForm1.aspx in the source view; add an ASP.NET button, and switch to the design view.

3. Workarounds

  • The equivalent workaround code for VB.NET is
           If Context IsNot Nothing AndAlso Context.Session IsNot Nothing Then
  • We can also check the existence of the property DesignMode instead of checking for Context and Context.Session. However, checking for the existence of Context and Context.Session method is more resilient since we’re checking the object before accessing it. Checking DesignMode would work in this scenario, but might not in other scenarios where DesignMode might not be set at the time of checking, or its value was not updated in cache.

Anh Phan

Leave a Comment
  • Please add 2 and 2 and type the answer here:
  • Post
  • Hi,

    Now i am getting that error.


    Jinal Patel.

  • Alex,

    Does the workaround mentioned by southwynd or JohnH work for you?

  • I'm seeing what I think NickH was seeing. Converted a project from ASP.NET 1.1 to Visual Studio 2010. (Tried 4.0, 3.5, 2.0 with the same results.) Certain pages load fine. Other pages get the "Error Creating Control - Object reference not set to an instance of an object." message for all asp controls (html controls show fine).

    I thought it might be related to the fact that we were using Infragistics, but I've upgraded the app to the latest versions (tried the 3.5 and 2.0 controls) with no sucess. Plus, some of the pages that work have the same infra controls on the pages that don't, so I'm not sure that's what it is.

    I've found that if I change the name of the "Inherits" file (to something that's wrong), and then change it back, suddenly the page works. But if I close and re-open the page it's bad again.

    I changed the namespace, and it also worked. This time it would work even if I closed and re-opened the page. But when I closed the project and re-opened...errors again.

    As with the other guys, everything works fine at run-time.

    Have you guys made any more discoveries on this stuff since the last posts? Thanks.

  • LukeT,

    Do you have code inside OnInit() method? If so, can you post your code here? Thanks.

  • NickH,

    Are you saying that a new page with the exact same content in the code behind file would work fine, but the page converted from VS 2008 would show error in the design time?

  • I have no code inside any OnInit() methods that I can find anywhere. I'll see if I can put together some simple code, but I know at one time I had the page stripped down to only an edit box (and did the same with another page that worked). One continued to show the error while the other worked. Which makes me think the real issue is outside the individual page...maybe a project setting or something? But something that is tied to that page name/class, etc?

    Somehow when I change the code-behind class name then it re-initializes something that otherwise is not being handled and then it works. Because I don't have to change ANY actual code in the code behind...I just have to tell the .aspx to point to something else...then when I change the code-behind class name back, the page still works. (Until I close and open it again.)

    I'm just wondering if you guys understand more of the rendering path behind the scenes, maybe you can figure out what paths it might take differently and that might point you to the issue.

    One thing I'm going off to try now...removing the file from the project, then re-adding it (so it's "new"). Maybe that will cause it to work? I'll let you know.

  • Can you send me the project that reproduces the issue to anhphan AT That would really help with the investigation. Thanks.

  • Just to clarify something above. It is NOT changing the CodeBehind name that causes the controls to render, but rather changing the Inherits name. I messed that up in my last post.

    I'll see what I can do about sending you some code.


  • The problem seems to be that VS 2008 didn't run the OnInit method in the actual page at design time, but VS 2010 does.

    If you have where events are wired up in the OnInit event (e.g. how VS 2003 used to do it, if you have apps upgraded from .NET 1.1), you get null reference exceptions all over the page. e.g.:

    this.registerButton.Click += new EventHandler(this.registerButton_Click);


    Even if you mode the event wirup after the base.OnInit, it still messes up the designer. This does seem to be a rather serious oversight, considering the page runs fine and VS 2008 didn't have this bug.

  • We have an additional blog discussing the OnInit() method being called at the design time,

    LukeT sent me the sample proeject and it turned out that he did have code inside the OnInit() method. Applying the workaround works fine for him.


  • I'm having the same problem as Luke and others in that I have a 2.0 framework project that works perfectly fine in VS2008.  I upgraded the project into VS10 and while the project builds fine, I cant open any of the web pages in the designer.  If I make a change to the "Inherits" line, it then magically renders correctly in the designer, however, If I close the page, and re-open it, again, its wont render.

    This is shocking to me that something of this magnitude was able to make it into the final version as this is a HUGE bug on the MSFT side.  I cant use VS 10 until this serious issue is corrected.

  • Update - my bad - if there is ANY code in the OnInit event, you need to use the workaround described by WebDevTools.  One note, make sure you re-build the site before attempting to re-open the page and it now renders.  A bit annoying that I have to add this code, but Ill take it for now!

  • This worked for me because after hours of searching i came across this page.

    The only question i have is why is there code running when you are looking at the page in the designer? Why are page events being fired at design time? I'm sure many more people will have the same headaches over this as i did.

  • This work for me:

    you have to check Session object and also Context object inside all Init routine:

    i.e. : implementation of

    a)   protected override void OnInit(EventArgs e)

    b)   void WPPBasePage_Init(object sender, System.EventArgs e)  registerd with :

                        this.init += new EventHandler(YourBasePage_Init);

  • Correction:

    void YourBasePage_Init(object sender, System.EventArgs e)  registerd with :

                       this.init += new EventHandler(YourBasePage_Init);

Page 2 of 3 (33 items) 123