{"id":3114,"date":"2010-04-15T15:56:00","date_gmt":"2010-04-15T15:56:00","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/webdev\/2010\/04\/15\/controls-state-error-creating-control-in-the-design-view-in-visual-studio-2010\/"},"modified":"2010-04-15T15:56:00","modified_gmt":"2010-04-15T15:56:00","slug":"controls-state-error-creating-control-in-the-design-view-in-visual-studio-2010","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/dotnet\/controls-state-error-creating-control-in-the-design-view-in-visual-studio-2010\/","title":{"rendered":"Controls State: &#8220;Error Creating Control&#8221; in the Design View in Visual Studio 2010"},"content":{"rendered":"<p>In VS 2010, if you are accessing the Session state in the OnInit(\u2026) method of your page, similar to the following code snippet:<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/16\/2019\/02\/OnInit_2.png\"><img decoding=\"async\" style=\"border-right-width: 0px;border-top-width: 0px;border-bottom-width: 0px;border-left-width: 0px\" title=\"OnInit\" border=\"0\" alt=\"OnInit\" src=\"https:\/\/devblogs.microsoft.com\/dotnet\/wp-content\/uploads\/sites\/10\/2010\/04\/OnInit_thumb_2.png\" width=\"978\" height=\"590\" \/><\/a> <\/p>\n<\/p>\n<p>then you will encounter an \u201cError Creating Control\u201d 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:<\/p>\n<p>\u201c<font color=\"#ff0000\"><strong>Error Creating Control<\/strong><\/font> \u2013 Button1     <br \/>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 &lt;configuration&gt;&lt;system.web&gt;&lt;httpModules&gt; section in the application configuration.\u201d.<\/p>\n<p>Screen shot of the error:<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/16\/2019\/02\/ErrorCreatingControl.png\"><img decoding=\"async\" style=\"border-right-width: 0px;border-top-width: 0px;border-bottom-width: 0px;border-left-width: 0px\" title=\"ErrorCreatingControl\" border=\"0\" alt=\"ErrorCreatingControl\" src=\"https:\/\/devblogs.microsoft.com\/dotnet\/wp-content\/uploads\/sites\/10\/2010\/04\/ErrorCreatingControl_thumb.png\" width=\"981\" height=\"634\" \/><\/a>&#160;<\/p>\n<p>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.<\/p>\n<p>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.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/16\/2019\/02\/Workaround.png\"><img decoding=\"async\" style=\"border-right-width: 0px;border-top-width: 0px;border-bottom-width: 0px;border-left-width: 0px\" title=\"Workaround\" border=\"0\" alt=\"Workaround\" src=\"https:\/\/devblogs.microsoft.com\/dotnet\/wp-content\/uploads\/sites\/10\/2010\/04\/Workaround_thumb.png\" width=\"940\" height=\"519\" \/><\/a> <\/p>\n<p>We are considering adding a fix for this issue during the VS 2010 SP1 timeframe.<\/p>\n<p><strong>UPDATE:<\/strong>     <br \/>There are some confusion from the readers about the post, so I\u2019m updating the blog to clarify the issue and the workarounds.<\/p>\n<p>1. Steps to reproduce the issue for a website<\/p>\n<ul>\n<li>Add a file Class1.cs under App_Code directory. <\/li>\n<li>Replace the content of the file with the following code:      <br \/><em>namespace ClassLibrary1        <br \/>{         <br \/>&#160;&#160;&#160; public class MyCustomPage : System.Web.UI.Page         <br \/>&#160;&#160;&#160; {         <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160; protected override void OnInit(EventArgs e)         <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160; {         <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; base.OnInit(e);&#160; <br \/><\/em><em>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; int count = Session.Count;        <br \/><\/em><em>&#160;&#160;&#160;&#160;&#160;&#160;&#160; }        <br \/>&#160;&#160;&#160; }         <br \/>}<\/em> <\/li>\n<li>In a webform with no code behind, replace the Page directive with the following line:      <br \/>&lt;%@ Page Language=&quot;C#&quot; Inherits=&quot;ClassLibrary1.MyCustomPage&quot; %&gt; <\/li>\n<li>Add an ASP.NET button, then switch to the design view. <\/li>\n<\/ul>\n<p>2. Steps to reproduce the issue for a Web Application Project (WAP)<\/p>\n<ul>\n<li>In the code behind file, WebForm1.asp.cs, add the following code snippet      <br \/><em>protected override void OnInit(EventArgs e)        <br \/>{         <br \/>&#160;&#160;&#160; base.OnInit(e);         <br \/>&#160;&#160;&#160; int count = Session.Count;         <br \/>}<\/em> <\/li>\n<li>Save the file and compile the project. <\/li>\n<li>Close and re-open the file WebForm1.aspx in the source view; add an ASP.NET button, and switch to the design view. <\/li>\n<\/ul>\n<p>3. Workarounds<\/p>\n<ul>\n<li>The equivalent workaround code for VB.NET is      <br \/><em>&#160;&#160;&#160;&#160;&#160;&#160; If Context IsNot Nothing AndAlso Context.Session IsNot Nothing Then        <br \/><\/em><\/li>\n<li>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\u2019re 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. <\/li>\n<\/ul>\n<p>Thanks.    <br \/>Anh Phan<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In VS 2010, if you are accessing the Session state in the OnInit(\u2026) method of your page, similar to the following code snippet: then you will encounter an \u201cError Creating Control\u201d 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 [&hellip;]<\/p>\n","protected":false},"author":404,"featured_media":58792,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[197],"tags":[],"class_list":["post-3114","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-aspnet"],"acf":[],"blog_post_summary":"<p>In VS 2010, if you are accessing the Session state in the OnInit(\u2026) method of your page, similar to the following code snippet: then you will encounter an \u201cError Creating Control\u201d 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 [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/dotnet\/wp-json\/wp\/v2\/posts\/3114","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/dotnet\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/dotnet\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/dotnet\/wp-json\/wp\/v2\/users\/404"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/dotnet\/wp-json\/wp\/v2\/comments?post=3114"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/dotnet\/wp-json\/wp\/v2\/posts\/3114\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/dotnet\/wp-json\/wp\/v2\/media\/58792"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/dotnet\/wp-json\/wp\/v2\/media?parent=3114"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/dotnet\/wp-json\/wp\/v2\/categories?post=3114"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/dotnet\/wp-json\/wp\/v2\/tags?post=3114"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}