0

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?

5
  • 3
    Don't use include files. Now we have UserControls and MasterPages that give us that functionality. I doubt the ASP.NET engine properly parses "include" files, and therefore the raw contents are sent to the client instead of the generated HTML. Commented May 23, 2014 at 14:08
  • I have a master page already and I am using one line inside the master page for each sub pages: <asp:ContentPlaceHolder runat="server" ID="FeaturedContent" /> and my about.aspx page is the whole featuredcontent. Inside the about.aspx how do I call another page? Commented May 23, 2014 at 14:10
  • The link I provided explains all of that. Try it out. If you have specific problems, feel free to post them to SO. Commented May 23, 2014 at 14:12
  • By the way, I'd suggest using the term ASP.NET instead of "ASP" to help differentiate this technology from "classic ASP". Commented May 23, 2014 at 14:33
  • @MarioJVargas Thanks. I just edited it. Great point! Commented May 23, 2014 at 14:39

1 Answer 1

2

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" />
Sign up to request clarification or add additional context in comments.

6 Comments

from the look of it, it seems the UserControl can also have standard HTML tag, which adding a runat=server can also be accessed by the code-behind like other aspx controls? Please correct me if I am wrong.
It should be noted that you can register controls in the web.config file so you don't have to manually register them on each page you want to use them on. Very helpful if you end up using the control more than once (which is the whole point of making it a control!)
@SiKni8 Yes, you can think of UserControls as reusable snippets of markup/code. Anything you can do on a page you can do on a UserControl. So you can make your regular HTML controls accessible on the server by adding runat="server" to them.
I can avoid this line <%@ Register tagPrefix="uc" tagName="RadHours" src="RadHours.ascx" %> in every page I need to use it in, If I register it in web.config?
@SiKni8 , yes you can access user controls from the code-behind of the referencing page. Treat them as you would any encapsulated class.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.