简体   繁体   English

为什么在ASP.NET中的包含文件中无法识别asp

[英]Why asp is not recognized in an include file in ASP.NET

I have an about.aspx page which calls an include file (because I want to reuse the file in my other aspx pages as well) like this: 我有一个about.aspx页面,它调用一个包含文件(因为我想在我的其他aspx页面中重用该文件),如下所示:

<!-- #include virtual ="includeNav/radHours.inc" -->

Inside the include file, I have a bunch of HTML/ASP.NET controls. 在include文件中,我有一堆HTML / ASP.NET控件。 Some of them are: 他们之中有一些是:

<asp:Label id="lhWP" runat="server" text="White Plains" />
<div id="dvWP" runat="server" style="width: 100%; text-align: center; padding-top: 10px;"></div>

I get the following error: 我收到以下错误:

Namespace prefix 'asp' not defined

Why do I get the error and how do I resolve it? 为什么我会收到错误,如何解决?

Create a UserControl, then reference it in the about.aspx page. 创建一个UserControl,然后在about.aspx页面中引用它。

In Visual Studio right-click on your project or a subfolder within it, then point to Add and select New Item... 在Visual Studio中右键单击项目或其中的子文件夹,然后指向添加并选择新项...

Then in the Add New Item dialog box select Web , then click on Web Forms User Control (It may simply say User Control ). 然后在“ 添加新项”对话框中选择“ Web” ,然后单击“ Web窗体用户控件” (可能只是说“ 用户控件” )。 Name it RadHours.ascx 将其命名为RadHours.ascx

Then paste the code from your SSI (server-side include) in the user control. 然后将SSI(服务器端包含)中的代码粘贴到用户控件中。 Please note that the Inherits property uses the namespace WebApplication1 . 请注意, Inherits属性使用名称空间WebApplication1 You will need to change this so it matches your namespace scheme. 您需要对其进行更改,使其与命名空间方案相匹配。 This is for illustration purposes. 这是出于说明目的。

<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="RadHours.ascx.cs" Inherits="WebApplication1.RadHours" %>

<asp:Label id="lhWP" runat="server" text="White Plains" />
<div id="dvWP" runat="server" style="width: 100%; text-align: center; padding-top: 10px;"></div>

Then in the about.aspx page, place the following line below the @Page directive: 然后在about.aspx页面中, 将以下行放在@Page指令下面:

<%@ Register tagPrefix="uc" tagName="RadHours" src="RadHours.ascx" %>

Finally, replace the #include directive you used with this: 最后,替换您使用的#include指令:

<uc:RadHours ID="radHours" runat="server" />

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM