Si8
Si8

Reputation: 9225

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:

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

Inside the include file, I have a bunch of HTML/ASP.NET controls. 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?

Upvotes: 0

Views: 863

Answers (1)

Mario J Vargas
Mario J Vargas

Reputation: 1195

Create a UserControl, then reference it in the about.aspx page.

In Visual Studio right-click on your project or a subfolder within it, then point to Add and select New Item...

Then in the Add New Item dialog box select Web, then click on Web Forms User Control (It may simply say User Control). Name it RadHours.ascx

Then paste the code from your SSI (server-side include) in the user control. Please note that the Inherits property uses the namespace 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:

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

Finally, replace the #include directive you used with this:

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

Upvotes: 2

Related Questions