Project type does not allow adding new "Web User Control"
Asked Answered
S

4

7

I have a mediocre skill level with VS and C#. I'm attempting to add a .ascx file with codebehind to an existing project in its user controls.

Right-clicking UserControls -> Add -> New Item and searching "web user control" returns "No items found."

Copy-pasting an existing .ascx file (there are more than a dozen) successfully creates the new .ascx file, but there's no .ascx.cs or .ascx.designer.cs files attached and no apparent way to add them.

More information

Copying "MyUserControl.ascx" was indeed copying the codebehind files, but they were copied under the original "MyUserControl.ascx" file as "MyUserControl - Copy.ascx.cs" and ""MyUserControl - Copy.ascx.designer.cs". VS does not allow these files to be renamed or moved.

So, I removed these files from the project, changed their names in file explorer, added them back to the project and updated the code as I needed.

However, they don't appear nested, but on the same level as the .ascx files.

Also tried

Created a blank web app and could add it there, so it's something project-specific.

Tried adding the file as existing after copying in file explorer, with the intent to generate the code behind files - there's no Convert to Web Application option, even when selecting the solution directly and even under the project menu in VS2015 for my project, as suggested by How do you force Visual Studio to regenerate the .designer files for aspx/ascx files?

Scythia answered 22/5, 2017 at 12:10 Comment(2)
You can't just copy and change names on the files. The classes and namespaces are still named as the originals and you should not edit generated code. I really doubt this will make a difference, but what if you search the item templates for "Web Forms User Control" instead?Frigate
@Frigate still not finding anything, which strikes me as exceedingly oddScythia
S
3

Now that I've posted a bounty...

Once the items (.ascx, .ascx.cs, and .ascx.designer.cs files) have been added to the project and their content updated to reflect desired changes (Codebehind attribute, class names, etc), simply edit your .projitems file for the project (I used notepad++ for this, not sure if it matters).

You'll find two lines that look something like this - search your file names to find easily:

<Compile Include="$(MSBuildThisFileDirectory)MyPath\UserControls\MyUserControl.ascx.designer.cs" />
<Compile Include="$(MSBuildThisFileDirectory)MyPath\UserControls\MyUserControl.ascx.cs" />

If you have other similar files you'll see that they have a dependent upon node. Adding this node and reloading in VS will get you what you need:

<Compile Include="$(MSBuildThisFileDirectory)MyPath\UserControls\MyUserControl.ascx.cs" >
  <SubType>ASPXCodeBehind</SubType>
  <DependentUpon>MyUserControl.ascx</DependentUpon>
</Compile>
<Compile Include="$(MSBuildThisFileDirectory)MyPath\UserControls\MyUserControl.ascx.designer.cs" >
  <DependentUpon>MyUserControl.ascx</DependentUpon>
</Compile>
Scythia answered 24/5, 2017 at 13:39 Comment(1)
There's a handy VS Extension called "Nested Items" that allows you to nest files in the Solution Explorer (exactly how you have by hand here) but through the UI. #RecommendedForb
H
7

It sounds like the main problem here is that you're unable to add a Web Forms User Controller using Visual Studio, so I will attempt to solve that.

I assume this is what you are already looking for, but this is what I do when I want to add a new Web Forms User Control. I right-click in Solution Explorer, I go to Add, and I click Web Forms User Control. enter image description here

However, I assume this isn't available to you. So the next place I would look is by selecting "New Item" when I right-click in Solution Explorer. Then, in the new add New Item dialog, I would go to Visual C# => Web => Web Forms User Control. enter image description here

If those two things are not options, then I would check to see that you have the additional web components installed into Visual Studio. It doesn't look like you said which version of Visual Studio you're using, so I am going to assume it's Visual Studio 2017. Open up your Visual Studio 2017 Installer, and click the Modify button. Choose ASP.NET and Web Development. These components should give you the template options. Even if you are not talking about VS17, the previous Visual Studio installers are similar- modify your installation and add the web components.

enter image description here

Lastly, especially if you have the option available to you in other projects, but not this one, I happen to know Visual Studio will hide options/templates that do not correspond to your project type. Open up your project's .csproj file in your favorite text editor (apparently NotePad++) and go to about line 13 where you should find a semicolon-delimited list of ProjectTypeGuids. In my example below, I have two: 349c5851-65df-11da-9384-00065b846f21 and fae04ec0-301f-11d3-bf4b-00c04f79efbc. The first represents ASP.NET MVC 5. The latter represents C#. The first guid is the reason Visual Studio considers this a web project. A list of project type guids are available here. enter image description here

Homologize answered 30/5, 2017 at 2:26 Comment(0)
S
3

Now that I've posted a bounty...

Once the items (.ascx, .ascx.cs, and .ascx.designer.cs files) have been added to the project and their content updated to reflect desired changes (Codebehind attribute, class names, etc), simply edit your .projitems file for the project (I used notepad++ for this, not sure if it matters).

You'll find two lines that look something like this - search your file names to find easily:

<Compile Include="$(MSBuildThisFileDirectory)MyPath\UserControls\MyUserControl.ascx.designer.cs" />
<Compile Include="$(MSBuildThisFileDirectory)MyPath\UserControls\MyUserControl.ascx.cs" />

If you have other similar files you'll see that they have a dependent upon node. Adding this node and reloading in VS will get you what you need:

<Compile Include="$(MSBuildThisFileDirectory)MyPath\UserControls\MyUserControl.ascx.cs" >
  <SubType>ASPXCodeBehind</SubType>
  <DependentUpon>MyUserControl.ascx</DependentUpon>
</Compile>
<Compile Include="$(MSBuildThisFileDirectory)MyPath\UserControls\MyUserControl.ascx.designer.cs" >
  <DependentUpon>MyUserControl.ascx</DependentUpon>
</Compile>
Scythia answered 24/5, 2017 at 13:39 Comment(1)
There's a handy VS Extension called "Nested Items" that allows you to nest files in the Solution Explorer (exactly how you have by hand here) but through the UI. #RecommendedForb
B
0

I converted my project from VB to C# and it was somehow treating it as WinForm project. Later I found out that ProjectTypeGuids mentioned in .csproj files determines the type of project.

ASP.NET 5 {8BB2217D-0F2D-49D1-97BC-3654ED321F3B}

ASP.NET MVC 1 {603C0E0B-DB56-11DC-BE95-000D561079B0}

ASP.NET MVC 2 {F85E285D-A4E0-4152-9332-AB1D724D3325}

ASP.NET MVC 3 {E53F8FEA-EAE0-44A6-8774-FFD645390401}

ASP.NET MVC 4 {E3E379DF-F4C6-4180-9B81-6769533ABE47}

ASP.NET MVC 5 {349C5851-65DF-11DA-9384-00065B846F21}

C# {FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}

C++ {8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}

Database {A9ACE9BB-CECE-4E62-9AA4-C7E7C5BD2124}

Database (other project types) {4F174C21-8C12-11D0-8340-0000F80270F8}

Deployment Cab {3EA9E505-35AC-4774-B492-AD1749C4943A}

Deployment Merge Module {06A35CCD-C46D-44D5-987B-CF40FF872267}

Deployment Setup {978C614F-708E-4E1A-B201-565925725DBA}

Deployment Smart Device Cab {AB322303-2255-48EF-A496-5904EB18DA55}

Distributed System {F135691A-BF7E-435D-8960-F99683D2D49C}

Dynamics 2012 AX C# in AOT {BF6F8E12-879D-49E7-ADF0-5503146B24B8}

F# {F2A71F9B-5D33-465A-A702-920D77279786}

J# {E6FDF86B-F3D1-11D4-8576-0002A516ECE8}

Legacy (2003) Smart Device (C#) {20D4826A-C6FA-45DB-90F4-C717570B9F32}

Legacy (2003) Smart Device (VB.NET) {CB4CE8C6-1BDB-4DC7-A4D3-65A1999772F8}

Micro Framework {b69e3092-b931-443c-abe7-7e7b65f2a37f}

Model-View-Controller v2 (MVC 2) {F85E285D-A4E0-4152-9332-AB1D724D3325}

Model-View-Controller v3 (MVC 3) {E53F8FEA-EAE0-44A6-8774-FFD645390401}

Model-View-Controller v4 (MVC 4) {E3E379DF-F4C6-4180-9B81-6769533ABE47}

Model-View-Controller v5 (MVC 5) {349C5851-65DF-11DA-9384-00065B846F21}

Mono for Android {EFBA0AD7-5A72-4C68-AF49-83D382785DCF}

MonoTouch {6BC8ED88-2882-458C-8E55-DFD12B67127B}

MonoTouch Binding {F5B4F3BC-B597-4E2B-B552-EF5D8A32436F}

Portable Class Library {786C830F-07A1-408B-BD7F-6EE04809D6DB}

Project Folders {66A26720-8FB5-11D2-AA7E-00C04F688DDE}

SharePoint (C#) {593B0543-81F6-4436-BA1E-4747859CAAE2}

SharePoint (VB.NET) {EC05E597-79D4-47f3-ADA0-324C4F7C7484}

SharePoint Workflow {F8810EC1-6754-47FC-A15F-DFABD2E3FA90}

Silverlight {A1591282-1198-4647-A2B1-27E5FF5F6F3B}

Smart Device (C#) {4D628B5B-2FBC-4AA6-8C16-197242AEB884}

Smart Device (VB.NET) {68B1623D-7FB9-47D8-8664-7ECEA3297D4F}

Solution Folder {2150E333-8FDC-42A3-9474-1A3956D46DE8}

Test {3AC096D0-A1C2-E12C-1390-A8335801FDAB}

Universal Windows Class Library {A5A43C5B-DE2A-4C0C-9213-0A381AF9435A}

VB.NET {F184B08F-C81C-45F6-A57F-5ABD9991F28F}

Visual Database Tools {C252FEB5-A946-4202-B1D4-9916A0590387}

Visual Studio 2015 Installer Project Extension {54435603-DBB4-11D2-8724-00A0C9A8B90C}

Visual Studio Tools for Applications (VSTA) {A860303F-1F3F-4691-B57E-529FC101A107}

Visual Studio Tools for Office (VSTO) {BAA0C2D2-18E2-41B9-852F-F413020CAA33}

Web Application {349C5851-65DF-11DA-9384-00065B846F21}

Web Site {E24C65DC-7377-472B-9ABA-BC803B73C61A}

Windows (C#) {FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}

Windows (VB.NET) {F184B08F-C81C-45F6-A57F-5ABD9991F28F}

Windows (Visual C++) {8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}

Windows Communication Foundation (WCF) {3D9AD99F-2412-4246-B90B-4EAA41C64699}

Windows Phone 8/8.1 Blank/Hub/Webview App {76F1466A-8B6D-4E39-A767-685A06062A39}

Windows Phone 8/8.1 App (C#) {C089C8C0-30E0-4E22-80C0-CE093F111A43}

Windows Phone 8/8.1 App (VB.NET) {DB03555F-0C8B-43BE-9FF9-57896B3C5E56}

Windows Presentation Foundation (WPF) {60DC8134-EBA5-43B8-BCC9-BB4BC16C2548}

Windows Store (Metro) Apps & Components {BC8A1FFA-BEE3-4634-8014-F334798102B3}

Workflow (C#) {14822709-B5A1-4724-98CA-57A101D1B079}

Workflow (VB.NET) {D59BE175-2ED0-4C54-BE3D-CDAA9F3214C8}

Workflow Foundation {32F31D43-81CC-4C15-9DE6-3FC5453562B6}

Xamarin.Android {EFBA0AD7-5A72-4C68-AF49-83D382785DCF}

Xamarin.iOS {6BC8ED88-2882-458C-8E55-DFD12B67127B}

XNA (Windows) {6D335F3A-9D43-41b4-9D22-F6F17C4BE596}

XNA (XBox) {2DF5C3F4-5A5F-47a9-8E94-23B4456F55E2}

XNA (Zune) {D399B71A-8929-442a-A9AC-8BEC78BB2433}

For more details please refer here

Breakwater answered 16/4, 2018 at 0:48 Comment(0)
V
0

My mistake

I choosed the wrong file with ascx ending (I thought it makes no differnce). You see the wrong item in the picture: enter image description here

My solution

I had to scroll more down to the right (Web Forms User Control) item, to be able to add it. See the right item in the picture: enter image description here

Ventricose answered 27/6, 2019 at 10:18 Comment(1)
I know i was kind of stupid becuse i just looked at the ending of the file. But maybe my solution will help somebody 😊Ventricose

© 2022 - 2024 — McMap. All rights reserved.