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

Web Development Tools Microsoft

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


Discussion is closed.

Feedback usabilla icon