1

I am working in VS2012 on ASP.NET Web Forms project. The project has Site.Master page which is used for all pages. I can add script to the Site.Master where I can write jQuery function; no problem. But how and where do you place iQuery script if I want it in a certain page of the project? The page (because it uses Site.Master) does not have Head section but has only one section:

<asp:Content runat="server" ID="BodyContent" ContentPlaceHolderID="MainContent">

If I write script before the above line I get VS problem "Only Content controls are allowed directly in contect page ..."

So where would I put the jQuery script specific to one page?

3 Answers 3

3

What you have to do is modify your site master page to have a content placeholder in the head section of the master page. Then in your regular aspx page that uses the master page put an asp:content object and then in the content object place your javascript code.

So you'll need a in your master page head:

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

and in your aspx page:

<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="Server">

</asp:Content>

But if you are looking for better performance I would put your scripts at the bottom of the page. You can read more about this here: http://developer.yahoo.com/performance/rules.html#js_bottom

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

5 Comments

Thank you all very much for your help. As far as for better performace to put the script to the bottom of the page, do you mean that the section asp:Content Head should be after the section asp:Content MainContent?
Meaning that the content place holder in the master page should be at the bottom of the master page but before the form close tag
I got it now. Thank you.
I am not sure I understand what you mean by "accept my answer." (I am a newbie in this forum, so please explain).
There is a check that is next to the vote score of the answer if you click that it will mark my answer as accepted.
1

You can put the Jquery into the asp:content for example:

<asp:Content runat="server" ID="BodyContent" ContentPlaceHolderID="MainContent">

<script>
//Your Jquery
</script>
</asp:Content>

The only that you will need register the Jquery libray in the Master page that the page will use.

I hope that helps.

Comments

0

You can add it programmatically like this from your page_load function:

HtmlGenericControl include = new HtmlGenericControl("script"); 
Include.Attributes.Add("type", "text/javascript"); 
Include.Attributes.Add("src", yourUrl); 
this.Page.Header.Controls.Add(include);

Comments

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.