最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

c# - What's the difference between <head> and <asp:Content ID="HeaderContent" ...

programmeradmin1浏览0评论

This may be a newbie question, but i'm pretty new to asp & C# etc.

I'm working with an ASP website, and I'm curious about the structure of it (after automatically creating a web project), specifically the following:

I see that in Default.aspx , I have a tag like this:

<asp:Content ID="HeaderContent" runat="server" ContentPlaceHolderID="HeadContent">
</asp:Content>

But in Site.master, I have this:

<head runat="server">
*etc*
</head>

So where would I put code if I wanted to include JavaScript code to run, on page load?

This may be a newbie question, but i'm pretty new to asp.net & C# etc.

I'm working with an ASP.net website, and I'm curious about the structure of it (after automatically creating a web project), specifically the following:

I see that in Default.aspx , I have a tag like this:

<asp:Content ID="HeaderContent" runat="server" ContentPlaceHolderID="HeadContent">
</asp:Content>

But in Site.master, I have this:

<head runat="server">
*etc*
</head>

So where would I put code if I wanted to include JavaScript code to run, on page load?

Share Improve this question edited Dec 20, 2024 at 15:30 General Grievance 4,98838 gold badges37 silver badges55 bronze badges asked Feb 20, 2013 at 16:31 CaffeinatedCaffeinated 12.5k41 gold badges127 silver badges225 bronze badges 1
  • 2 On every page that uses that master file, or just on Default.aspx? Btw, if you want to run JavaScript on page load, then you can put that JavaScript at the bottom of the page (which aids page performance). – Šime Vidas Commented Feb 20, 2013 at 16:35
Add a comment  | 

4 Answers 4

Reset to default 7

I believe you can put your code in any of them. The first one is for adding code or script used by all content pages(that using this master page file) while the second one is if you want to to add script or code from content pages(that should be used only for this specific page)

//in the Master page, the content here is used by all content pages
<head runat="server">
*etc*
</head>

and

//this is specific to the content page that use it. This section needs to be supplied in content pages
<asp:Content ID="HeaderContent" runat="server" ContentPlaceHolderID="HeadContent">
</asp:Content>

That section needs to be supplied in each content page and it will be exclusive to that page - no other page can use the script in that section

asp:Content ID="HeaderContent" is a content region. Anything within that tag will get embedded in the associated ContentPlaceHolder in the master page when it is generated.

head is a standard html markup, indicating the page head elements. Typically, the HeadContent placeholder is inside the head tag on the master page.

The head element, container for all the head elements, must use a title for the document. Some other elements it can include: style, base, link, meta, script, noscript.

The asp: Content ID = "HeaderContent" is a content element of the master page. Have a look at the Plugging in Content part of the following link for detailed information on this: http://odetocode.com/articles/419.aspx

I think you asked when you want to use JavaScript where you put JS in your code.You can put anywhere you wish in asp side between script block such as:

<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="server">
     <script type="text/javascript">
        function Onclick(){
             //some codes
        }
     </script>
</asp:Content>

or

    <head runat="server">
     <script type="text/javascript">
        function Onclick(){
             //some codes
        }
     </script>
   </head>

Also you can put JS outside this tag. You only should use tag.

发布评论

评论列表(0)

  1. 暂无评论