Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Project type does not allow adding new "Web User Control"

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?

like image 304
Randy Hall Avatar asked May 22 '17 12:05

Randy Hall


2 Answers

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

like image 175
Sam Rueby Avatar answered Nov 09 '22 13:11

Sam Rueby


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>
like image 41
Randy Hall Avatar answered Nov 09 '22 12:11

Randy Hall