0

I'm currently building from scratch an ASP.NET with C# website, using Visual Studio. I have an aspx file that references a couple of user controls (ascx) like this:

<%@ Register src="UserControls/CategoriesList.ascx" tagname="CategoriesList" tagprefix="uc2" %>

I am interested in customizing some elements on the page (e.g. labels, etc), so I included (immediately after the above line) a reference to my .CSS file:

<link rel="Stylesheet" type="text/css" href="Cinemax.css" />

After doing so, I got an error message:

Parser Error Message: Only Content controls are allowed directly in a content page that contains Content controls.

The error specifically points to the <link rel.... (line of code inserted above.)

Is there a workaround so I can include [in the same file!] references to user controls, as well as to a CSS stylesheet? Many thanks in advance.

4
  • 1
    I suppose that you should put the line including css to the file in <head></head> section of the page. Commented Jun 2, 2012 at 10:00
  • 1
    Excellent, thank you, Lucas, that was the solution! In fact, since I didn't find any body or head tag in this file, I went back to the master page and added the CSS reference there. Worked like a charm! Commented Jun 2, 2012 at 10:08
  • Indeed, Nikhil, I am using a master page, why do you ask? Commented Jun 2, 2012 at 10:09
  • I'm glad it helped :). I'll add this as an answer, but I haven't finished writing it yet :). Commented Jun 2, 2012 at 10:10

4 Answers 4

2

Seems like you are using a master page. Master page has

<asp:ContentPlaceHolder 

tags. Your page in question overrides content of these tags by using

<asp:Content 

tags.

By default, your master page should have something like this (a reference to the head section of the page):

<asp:ContentPlaceHolder runat="server" ID="HeadContent" />

To reference you stylesheet from the page you are talking about, you would do this:

<asp:Content runat="server" ID="Content" ContentPlaceHolderID="HeadContent">
    <link rel="Stylesheet" type="text/css" href="Cinemax.css" />
</asp:Content>

because css references must reside in the head section. And as for the error you are getting, this is because if a page has a master page, it cannon have any content outside of the

<asp:ContentPlaceHolder 

tags.

Sign up to request clarification or add additional context in comments.

Comments

1

I suppose that you should put the line including css to the file in <head></head> section of the page, so your file can look like this:

<%@ Register src="UserControls/CategoriesList.ascx" tagname="CategoriesList" tagprefix="uc2" %>

<html>
<head>
    <link rel="Stylesheet" type="text/css" href="Cinemax.css" />
</head>
<body>
    ...
</body>
</html>

Update: You wrote you use master page, so putting it in the head section there should solve the issue. It worked for you, so I'm writing it, so it can also be helpful for the other people who have a similar issue.

Comments

1

You are using a master page and you do not have place all the content inside <asp:Content controls, and so you get this message.

Ensure that all your content is inside content controls that come from master page.

Comments

1

If you are referring to <asp:Content /> tags, you can't put anything outside of them in an .aspx page. So you're limited to putting them inside the tag.

If you want <script /> and <link /> tags you need to either put a <asp:ContentPlaceHolder /> in the <head> of your master page, or add them dynamically via the Page's Controls collection.

1 Comment

There was an issue with text formatting in your answer and all the text looked like a source code, so I fixed it. I suppose an extra ContentPlaceHolder in not needed in this case, because just one css for all the pages defined directly in master page is satisfying. Anyway, it's a worth noticing that this can also be done with a ContentPlaceHolder :).

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.