{"id":5344,"date":"2006-08-15T17:07:00","date_gmt":"2006-08-15T17:07:00","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/webdev\/2006\/08\/15\/part-2-of-3-creating-shared-user-controls-and-master-pages-with-sub-projects\/"},"modified":"2006-08-15T17:07:00","modified_gmt":"2006-08-15T17:07:00","slug":"part-2-of-3-creating-shared-user-controls-and-master-pages-with-sub-projects","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/dotnet\/part-2-of-3-creating-shared-user-controls-and-master-pages-with-sub-projects\/","title":{"rendered":"Part 2 of 3: Creating shared user controls and master pages with sub-projects"},"content":{"rendered":"<p><P>This is the&nbsp;Part 2&nbsp;of a 3 part&nbsp;series on using&nbsp;sub-projects with the Web Application Projects add-in&nbsp;for Visual Studio 2005.&nbsp; <\/P>\n<P>Part 1&nbsp;of the series can be found <A href=\"http:\/\/blogs.msdn.com\/webdevtools\/archive\/2006\/07\/01\/652986.aspx\">here<\/A>.&nbsp; <\/P>\n<P>I&nbsp;apologize to folks for the delay in getting this second post out, we&#8217;ve been super busy the past few weeks.&nbsp; This post covers two concepts when using sub-projects with the Web Application Projects add-in:<\/P>\n<UL>\n<LI>Creating a shared master page in the root project and&nbsp;using it in sub-projects \n<LI>Creating a shared user control in the root project and&nbsp;using it sub-projects<\/LI><\/UL>\n<P><FONT size=\"5\">Prerequisite<\/FONT><\/P>\n<UL>\n<LI>Visual Studio 2005 with <A href=\"http:\/\/g.msn.com\/9SE\/1?http:\/\/msdn.microsoft.com\/asp.net\/reference\/infrastructure\/wap\/default.aspx&amp;&amp;DI=6244&amp;IG=96cf4f0cef7744eaa8801eb0e9d345a4&amp;POS=1&amp;CM=WPU&amp;CE=1&amp;CS=OTH&amp;SR=1\">Web Application Projects add-in<\/A> installed \n<LI>This article&nbsp;assumes&nbsp;the user&nbsp;has already&nbsp;done the steps described in the&nbsp;<A href=\"http:\/\/blogs.msdn.com\/webdevtools\/archive\/2006\/07\/01\/652986.aspx\">Part 1<\/A>&nbsp;of this series&nbsp;which sets up&nbsp;a root project and sub-project in Visual Studio 2005.&nbsp;&nbsp; <STRONG><FONT color=\"#ff0000\">NOTE: It is very important to have done these prerequisite&nbsp;steps as the examples below will not work otherwise.<\/FONT><\/STRONG><\/LI><\/UL>\n<P><FONT size=\"5\">Creating a shared master page and using it in sub-projects<\/FONT><\/P>\n<P>In this example, i&#8217;ll show how to setup a master page in the root project and then use that master page in sub-projects in the same solution.<\/P>\n<P>1) The first step is to create a master page in the root project.&nbsp; To do this right-click on the root project&#8217;s node in solution explorere and select <EM>Add New Item<\/EM>.&nbsp; In the dialog select the&nbsp;<EM>Master Page<\/EM> item and provide a name for it (e.g. &#8211; RootProject.master).<\/P>\n<P><IMG src=\"https:\/\/msdnshared.blob.core.windows.net\/media\/MSDNBlogsFS\/prod.evol.blogs.msdn.com\/CommunityServer.Components.PostAttachments\/00\/00\/70\/15\/94\/part2-1.PNG\" border=\"0\"><\/P>\n<P>2) Once the master page is created in the root project, edit it <EM>Design<\/EM> view and add some default content as shown below.<\/P>\n<P><IMG src=\"https:\/\/msdnshared.blob.core.windows.net\/media\/MSDNBlogsFS\/prod.evol.blogs.msdn.com\/CommunityServer.Components.PostAttachments\/00\/00\/70\/15\/99\/part2-2.PNG\" border=\"0\"><\/P>\n<P>3) Now that we have a master page created in the root project, we&nbsp;need to create a content page in the&nbsp;sub-project which will use the master page from the root project.&nbsp; To do this right-click on the&nbsp;sub-project&#8217;s node in solution explorere and select <EM>Add New Item.<\/EM>&nbsp; In the dialog&nbsp;select the <EM>Web Form<\/EM>&nbsp;item and provide a name for it (e.g. &#8211; ContentPage.aspx).<\/P>\n<P><IMG src=\"https:\/\/msdnshared.blob.core.windows.net\/media\/MSDNBlogsFS\/prod.evol.blogs.msdn.com\/CommunityServer.Components.PostAttachments\/00\/00\/70\/16\/00\/part2-3.PNG\" border=\"0\"><\/P>\n<P>4) The next step is to convert the web form created in the sub-project to a content page.&nbsp; To do this first remove all the content from the Web Form so it only has the <EM>Page<\/EM> directive line left as shown below.<\/P>\n<P><IMG src=\"https:\/\/msdnshared.blob.core.windows.net\/media\/MSDNBlogsFS\/prod.evol.blogs.msdn.com\/CommunityServer.Components.PostAttachments\/00\/00\/70\/16\/01\/part2-4.PNG\" border=\"0\"><\/P>\n<P>5) Next add in a M<EM>asterPageFile<\/EM> attribute and&nbsp;one&nbsp;or more&nbsp;asp.net&nbsp;content control to use the master page from the root project as shown in the diagram below.&nbsp; In the <EM>MasterPageFile<\/EM> attribute, refer to the master page in the root project using a relative path from the current document.&nbsp; Add as many content controls as &#8220;contentplaceholder&#8221; areas in the master page that you would like to override.<\/P>\n<P><STRONG>Note:<\/STRONG> Visual Studio 2005 will indicate an error on the <EM>MasterPageFile<\/EM> and <EM>ContentPlaceHolderID<\/EM> attributes.&nbsp; Both of these are known bugs in Visual Studio and can be ignored as they really aren&#8217;t errors when the page is run.<\/P>\n<P><IMG src=\"https:\/\/msdnshared.blob.core.windows.net\/media\/MSDNBlogsFS\/prod.evol.blogs.msdn.com\/CommunityServer.Components.PostAttachments\/00\/00\/70\/16\/02\/part2-5.PNG\" border=\"0\"><\/P>\n<P>6) If&nbsp;one switches to <EM>Design<\/EM> view on the content page in the sub-project, you will notice that only the content from the content page renders and the master page itself does not render.&nbsp; This also is a known limitation of Visual Studio 2005.&nbsp; Although the master page portion of the content is not shown in <EM>Design<\/EM> view when using a master page from a root project, the content page can still be fully edited like any other page, so this limitation should not be too impactful to most users.<\/P>\n<P><IMG src=\"https:\/\/msdnshared.blob.core.windows.net\/media\/MSDNBlogsFS\/prod.evol.blogs.msdn.com\/CommunityServer.Components.PostAttachments\/00\/00\/70\/16\/03\/part2-6.PNG\" border=\"0\"><\/P>\n<P>7) To test that the master page is actually working with the content page, right click on the content page in the sub-project.&nbsp;and select <EM>View in Browser<\/EM>.&nbsp; This will launch the web broswer and show the content page merged with the master page from the root project.<\/P>\n<P><IMG src=\"https:\/\/msdnshared.blob.core.windows.net\/media\/MSDNBlogsFS\/prod.evol.blogs.msdn.com\/CommunityServer.Components.PostAttachments\/00\/00\/70\/16\/04\/part2-7.PNG\" border=\"0\"><\/P>\n<P><FONT size=\"5\">Creating a shared user control and using it in in sub-projects<\/FONT><\/P>\n<P>The previous section described how to create a shared master page in the root project and then how to use that master page with content pages in sub-projects.&nbsp; This section describes how to use the similar technique but to create a&nbsp;shared user control in the root project which can be&nbsp;used on pages&nbsp;in sub-projects.<\/P>\n<P>1) The first step is to create&nbsp;the shared user control in the root project.&nbsp; To do this right-click on the root project&#8217;s node in solution explorere and select <EM>Add New Item<\/EM>.&nbsp; In the dialog select the&nbsp;<EM>Web User Control<\/EM>&nbsp;item and provide a name for it (e.g. &#8211; RootUserControl.ascx).<\/P>\n<P><IMG src=\"https:\/\/msdnshared.blob.core.windows.net\/media\/MSDNBlogsFS\/prod.evol.blogs.msdn.com\/CommunityServer.Components.PostAttachments\/00\/00\/70\/16\/05\/part2-8.PNG\" border=\"0\"><\/P>\n<P>2) Once the&nbsp;user control&nbsp;is created in the root project, edit it <EM>Design<\/EM> view and add some default content as shown below.<\/P>\n<P><IMG src=\"https:\/\/msdnshared.blob.core.windows.net\/media\/MSDNBlogsFS\/prod.evol.blogs.msdn.com\/CommunityServer.Components.PostAttachments\/00\/00\/70\/16\/06\/part2-9.PNG\" border=\"0\"><\/P>\n<P>3)&nbsp;Now that&nbsp;the shared user control is created in the root project, the next step is to use it within a page inside of a sub-project.&nbsp; When using a user control from another project, Visual Studio 2005 does not provide drag\/drop support (i.e. &#8211; you can&#8217;t drag the ASCX file to the design surface), so one must manually type in a <EM>Register<\/EM> directive and add an instance of the user control.&nbsp; To do this, modify a page in the sub-project (default.aspx in this case), as shown in the diagram below.&nbsp; For the <EM>Src<\/EM> attribte of the Register directive use a relative path to refer to the shared user control in the root project.<\/P>\n<P><STRONG>Note:<\/STRONG> you may see several compile errors in the task list at this step.&nbsp; Those can be ignored for now as the next step addresses them.<\/P>\n<P><IMG src=\"https:\/\/msdnshared.blob.core.windows.net\/media\/MSDNBlogsFS\/prod.evol.blogs.msdn.com\/CommunityServer.Components.PostAttachments\/00\/00\/70\/16\/08\/part2-10.PNG\" border=\"0\"><\/P>\n<P>4) To use the shared user control in the sub-project, one additional step is requried.&nbsp; The sub-project will need a project&nbsp;reference to the root project.&nbsp; To do this right-click on the sub-project&#8217;s node in solution explorer and select <EM>Add Reference.<\/EM>&nbsp; In the <EM>Add Refernece<\/EM> dialog go to the <EM>Projects<\/EM> tab and select the root project to reference (MyLargeWeb in this example).<\/P>\n<P><IMG src=\"https:\/\/msdnshared.blob.core.windows.net\/media\/MSDNBlogsFS\/prod.evol.blogs.msdn.com\/CommunityServer.Components.PostAttachments\/00\/00\/70\/19\/00\/part2-14.PNG\" border=\"0\"><\/P>\n<P>5) As long as the output folder of the sub-project is set to the target the <EM>\\bin<\/EM> of the root project, then just adding a project reference to the root project should be&nbsp;enough and this step can be skipped.&nbsp; However if the output directory of the&nbsp;sub-project project is set anywhere else, then one should bring up the sub-project&#8217;s references list and modify the reference to the root project&nbsp;in the property grid and set its&nbsp;<EM>Copy Local<\/EM>&nbsp;property&nbsp;to <STRONG>False<\/STRONG> as shown below.<\/P>\n<P><IMG src=\"https:\/\/msdnshared.blob.core.windows.net\/media\/MSDNBlogsFS\/prod.evol.blogs.msdn.com\/CommunityServer.Components.PostAttachments\/00\/00\/70\/16\/11\/part2-12.PNG\" border=\"0\"><\/P>\n<P>7)&nbsp; After the reference to the root project has been added, switching to&nbsp;<EM>Design<\/EM> view&nbsp;for pages in&nbsp;sub-projects should render controls from the root project.&nbsp; Selecting a page in a sub-project that uses a user control from the root project and running it should work fine in the web browser also.<\/P>\n<P><IMG src=\"https:\/\/msdnshared.blob.core.windows.net\/media\/MSDNBlogsFS\/prod.evol.blogs.msdn.com\/CommunityServer.Components.PostAttachments\/00\/00\/70\/16\/12\/part2-13.PNG\" border=\"0\"><\/P>\n<P><STRONG>Note:<\/STRONG>&nbsp;you&nbsp;will still continue to get&nbsp;markup validation errors&nbsp;against some of the attributes&nbsp;on pages opened for editing which are using shared user controls from the root project.&nbsp; Similar to&nbsp;the master pages scenario in the last section, those validation errors are bugs in Visual Studio 2005 and since these are not compile errors they can be ignored.<\/P>\n<P><STRONG>Additional&nbsp;Note:<\/STRONG> there is a known intermittment bug when using the Web Application Projects add-in&nbsp;where switching a page to&nbsp;<EM>Design<\/EM> view&nbsp;may cause the IDE to hang.&nbsp; The bug&nbsp;is very intermittment and may or may not&nbsp;occur&nbsp;when doing&nbsp;a sub-project&nbsp;scenario with Web Application Projects.&nbsp; The product team has identified a fix for this issue and it will be fixed in the next servicing release of Visual Studio 2005.&nbsp; If you do happen to encounter this issue,&nbsp;and are blocked by it, please contact me via this blog or directly at omark-at-microsoft-dot-com and&nbsp;we will make a hotfix available to you.<\/P>\n<P><FONT size=\"5\">Summary<\/FONT><\/P>\n<P>As is described in this post, creating and using shared master pages and user controls is fairly straightforward when using the Web Application Projects add-in.&nbsp; There are a few extra steps one needs to take but other than that the pattern is fairly similar to how master pages and user controls are used within a single project.<\/P>\n<P>Looking forward to hearing your feedback again on this topic, and I will try to get Part 3 of the series done in shorter time than it took for this one ;-).<\/P>\n<P>&#8212; Omar<\/P><\/p>\n","protected":false},"excerpt":{"rendered":"<p>This is the&nbsp;Part 2&nbsp;of a 3 part&nbsp;series on using&nbsp;sub-projects with the Web Application Projects add-in&nbsp;for Visual Studio 2005.&nbsp; Part 1&nbsp;of the series can be found here.&nbsp; I&nbsp;apologize to folks for the delay in getting this second post out, we&#8217;ve been super busy the past few weeks.&nbsp; This post covers two concepts when using sub-projects with [&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":[31,7264],"class_list":["post-5344","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-aspnet","tag-asp-net","tag-wap"],"acf":[],"blog_post_summary":"<p>This is the&nbsp;Part 2&nbsp;of a 3 part&nbsp;series on using&nbsp;sub-projects with the Web Application Projects add-in&nbsp;for Visual Studio 2005.&nbsp; Part 1&nbsp;of the series can be found here.&nbsp; I&nbsp;apologize to folks for the delay in getting this second post out, we&#8217;ve been super busy the past few weeks.&nbsp; This post covers two concepts when using sub-projects with [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/dotnet\/wp-json\/wp\/v2\/posts\/5344","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=5344"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/dotnet\/wp-json\/wp\/v2\/posts\/5344\/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=5344"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/dotnet\/wp-json\/wp\/v2\/categories?post=5344"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/dotnet\/wp-json\/wp\/v2\/tags?post=5344"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}