Error creating control: Request is not available in this context

Working on an existing project, I suddenly noticed that after adding an aspx page and integrating it in the existing framework, I could not see any controls in the designer, and each control was being displayed as a grey box saying

Error creating control: <control name>
Request is not available in this context

After searching the webs for hours without any effect (and resetting the toolbox, deleting temp files, deleting the VS2010 cache below the user’s application data directory), I found this MSDN blog explaining what’s going on:

The OnInit method of a page or control must not access the Request object in Design Mode.

Indeed, part of the “integration” work included deriving the new page from a base class, which of course did NOT check for design mode.

After placing the lines

if (DesignMode)

in OnInit and other methods I thought might be relevant and recompiling the application and closing and re-opening the forms, the page actually displayed correctly.

Worse, the German translation of the error message is misleading:

Fehler beim Erstellen des Steuerelements: <control name>
Anforderung steht in diesem Kontext nicht zur Verfügung

as it translates the member variable [HttpContext.]Request into the word Anforderung, which obscures the hint to check for uses of Request in the code.

If I had used the Session object, the overwhelmingly verbose error message wouldn’t tell me anything useful

Der Sitzungszustand kann nur verwendet werden, wenn enableSessionState entweder in einer Konfigurationsdatei oder in der Seitendirektive auf “true” festgelegt wurde. Stellen Sie auch sicher, dass System.Web.SessionStateModule oder ein benutzerdefiniertes Sitzungzustandsmodul in der Anwendungskonfiguration im <configuration>\<system.web>\<httpModules>-Abschnitt enthalten ist.

The English original says:

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.

Again, Session and Page are programming language terms, and should not be translated.

No mention of OnInit, no mention that the exception is actually caused by the page, as the errors are displayed inside the control boxes.

11 Responses to Error creating control: Request is not available in this context

  1. etem says:

    Good catch

  2. Njål Eide says:

    Thank you so much for sharing. You saved me. Was driving me absolutely mad. Never entered my mind that it was the OnInit() code that caused it.

  3. Manuel says:

    Great article,

    this saved me hours of debugging.

    Thank you

  4. jaraics says:

    Good post. Thank you!

  5. Gee says:

    It works for me… Thanks 🙂

  6. 600rr says:

    Nice Find!!!

  7. wonderfull Work says:

    Thank heaps..

  8. Hi says:

    Thanks, This works!!

  9. Pablo Faria says:

    Thanks man! It is a great article and save me!!!!

  10. manuel says:

    thanks a lot! this saved me a lot of thime and exactly solved my problem. 🙂

  11. gud job !!! u had saved so many developer outside there.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: