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

时间:2014-05-23 14:04:42

标签: c# html asp.net

我有一个about.aspx页面,它调用了一个包含文件(因为我想在我的其他aspx页面中重用该文件),如下所示:

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

在include文件中,我有一堆HTML / ASP.NET控件。其中一些是:

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

我收到以下错误:

Namespace prefix 'asp' not defined

为什么我会收到错误,如何解决?

1 个答案:

答案 0 :(得分:2)

创建UserControl,然后在 about.aspx 页面中引用它。

在Visual Studio中右键单击项目或其中的子文件夹,然后指向添加并选择新项目...

然后在添加新项对话框中选择网络,然后点击 Web表单用户控件(可能只是说用户控制)。将其命名为 RadHours.ascx

然后将SSI(服务器端包含)中的代码粘贴到用户控件中。请注意,Inherits属性使用命名空间WebApplication1。您需要对其进行更改,使其与命名空间方案相匹配。这仅用于说明目的。

<%@ 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>

然后在 about.aspx 页面中,将以下行放在@Page指令下方:

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

最后,将您使用的#include指令替换为:

<uc:RadHours ID="radHours" runat="server" />
相关问题