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:

OnInit

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:

ErrorCreatingControl 

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.

Workaround

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

UPDATE:
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)
            {
                base.OnInit(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)
    {
        base.OnInit(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.

Thanks.
Anh Phan

Leave a Comment
  • Please add 6 and 5 and type the answer here:
  • Post
  • Wouldn't it be better to check the DesignMode property instead?

  • Thats a very nice tip.

    Thanks,

    Thani

  • Hello,

    I created WebApplication Project but i am not able to reproduce it.

    Can you please tell me that is there any specific setting create such problem ?

    Thanks,

    Jinal Patel.

  • There's no specific setting other than accessing the Session state in the OnInit method. Are you using VS 2010 RTM?

  • Hi,

    Yes i tested it on trail version of Visual Studio 2010 Ultimate edition but i am not able to reproduce it. Control are visible in designer view also and there is no such error.

    Thanks,

    Jinal Patel.

  • I am getting this error with the release version of VS 2010. I'm using VB and do access Session in the Page_Init event. What is the VB code I need to include, and should it come before or after the 'InitializeComponent' call?

    Thanks

    John H

  • I'm also getting the "Error Creating Control" in Design View on vs2010 (worked fine in 2008 and page runs fine), but not with the session-related error. I'm getting "object not set to an instance of an object".

    Any ideas?

  • Hi John,

    The equivalent code in VB.NET would look like:

    Protected Overrides Sub OnInit(ByVal e As System.EventArgs)

      InitializeComponent()

      MyBase.OnInit(e)

      If Context IsNot Nothing AndAlso Context.Session IsNot Nothing Then

          Dim count As Integer = Session.Count

      End If

    End Sub

    Thanks.

  • Hi Nick,

    Do you have any code in the code-behind file? If yes, can you post the code here. Thanks.

  • Hi,

    Can you please put your sample project that cause this error over here ?

    I tried following way

    1. Test with Website Project

    2. Test With WebApplication Project with default application

    3. Test with Emply WebApplication Project.

    Previously i installed RC release on same machine. When i installed trial version i uninstalled RC release.

    None of page i am getting this error.

  • @WebDevTools

    I am unfortunately unable to release any piece the code due to legal agreements. It seems that new pages added to the project behave fine, but I cannot find any difference between the existing ones and the new ones. I ended up just reverting the project back to vs2008 for now. Of note, it is a Web Application project (originally written in vs2003, with .net 1.1) that has been updated to vs2008 and .net 2.0. I have not done the "Convert to Web Application" on all pages, but even pages that are converted, with designer file, still exhibited the same issue. Again, the code still runs and displays fine when compiled.

    @Jinal

    If you were talking to me, I'm guessing a new project would be fine as new pages in the same project are fine. I have no clue what the issue is, but don't have anymore time at the moment to continue fighting with vs2010.

  • Please Help !!!

    Most of my pages inherit class "MembersPage" and inside of this class i use this:

    Protected Overrides Sub OnInit(ByVal e As System.EventArgs)      

    If Session("UserID") = 0 Then

                   Response.Redirect("login.aspx")

               Else

                   MyBase.OnInit(e)

    End If

    Before i was getting design error about session state. After i've check about "designmode", I get error "Object Reference not set"

    Please Help !!! It goes to the point where i can't use VS 2010 !!!

  • I am getting this same error.  Here is the workaround I am using:

    Protected Sub Page_Init(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Init

       If Not DesignMode Then

            'your code here

       End If

    End Sub

    After adding the "If Not DesignMode" code, close the Designer, save your changes and Rebuild project.  Reopen the designer and the errors should be gone.

  • I was confused somewhat by the initial post. What I have found, which may well be what was meant, is that, in my Page_Init code, I have to include the check on Session actually existing around any code accessing Session, i.e. it is not sufficient to check for a particular session variable being nothing. E.g. for Alex above

    If Context IsNot Nothing AndAlso Context.Session IsNot Nothing Then

      If Session("UserID") = 0 THEN

          Response.Redirect("Login.aspx")

      Else

          MyBase.OnInit(e)

      End If

    End If

    (may need another 'else' if it is possible Session hasn't been created at runtime, but in my case this will never happen)

    Basically, it seems the init code is run 'stand-alone' when opening the .aspx page in the designer, so the code must be capable of running stand-alone with no errors.

    I would expect Southwynd's code would work just as well, and be simpler, but I hadn't read his post when I solved my problem.

  • Jinal,

    I've updated the blog with the repro steps. Hope it'll be clear for you.

    JohnH,

    The issue was at design time, some objects like Session, are not available. So we need to check for its existence before trying to access it.

    Thanks.

Page 1 of 3 (33 items) 123