Ran*_*all 5 c# asp.net user-controls
我在VS和C#方面的技能水平中等。我正在尝试将带代码隐藏的.ascx文件添加到其用户控件中的现有项目中。
右键单击UserControls-> Add-> New Item,然后搜索“ Web用户控件”,将返回“未找到任何项目”。
复制粘贴现有的.ascx文件(有十几个)成功创建了新的.ascx文件,但是没有附加.ascx.cs或.ascx.designer.cs文件,也没有明显的添加方法。
更多信息
复制“ MyUserControl.ascx”确实是在复制代码隐藏文件,但它们是在原始“ MyUserControl.ascx”文件下复制为“ MyUserControl-Copy.ascx.cs”和“ MyUserControl-Copy.ascx.designer.cs”。 VS不允许将这些文件重命名或移动。
因此,我从项目中删除了这些文件,在文件资源管理器中更改了它们的名称,将它们添加回项目中,并根据需要更新了代码。
但是,它们似乎没有嵌套,但与.ascx文件处于同一级别。
也尝试过
创建了一个空白的Web应用程序,可以将其添加到此处,因此它是特定于项目的。
尝试在文件浏览器中复制后将文件添加为现有文件,目的是生成文件后的代码- 即使直接选择解决方案,甚至在我的项目的VS2015项目菜单下,也没有“ 转换为Web应用程序”选项,如建议的那样通过如何强制Visual Studio为aspx / ascx文件重新生成.designer文件?
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.

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.

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.
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.

既然我已经发布了赏金...
将项目(.ascx、.ascx.cs 和 .ascx.designer.cs 文件)添加到项目并更新其内容以反映所需的更改(代码隐藏属性、类名等)后,只需编辑您的 .projitems项目的文件(我为此使用了记事本++,不确定它是否重要)。
您会发现两行看起来像这样 - 搜索文件名以轻松找到:
<Compile Include="$(MSBuildThisFileDirectory)MyPath\UserControls\MyUserControl.ascx.designer.cs" />
<Compile Include="$(MSBuildThisFileDirectory)MyPath\UserControls\MyUserControl.ascx.cs" />
Run Code Online (Sandbox Code Playgroud)
如果您有其他类似的文件,您将看到它们有一个依赖节点。添加这个节点并在 VS 中重新加载将得到你所需要的:
<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>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3203 次 |
| 最近记录: |