繁体   English   中英

有什么区别 <head> 和 <asp:Content ID=“HeaderContent” …> ?

[英]What's the difference between <head> and <asp:Content ID=“HeaderContent” …>?

这可能是一个新手问题,但我对asp.net和C#等很新。

我正在使用ASP.net网站,我很好奇它的结构(在自动创建一个Web项目之后),特别是以下内容:

我在Default.aspx中看到,我有一个这样的标签:

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

但在Site.master中,我有这个:

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

那么,如果我想在页面加载时包含要运行的JavaScript代码,我会在哪里放置代码?

我相信你可以把你的代码放在其中任何一个。 第一个用于添加所有内容页面(使用此母版页文件)使用的代码或脚本,而第二个用于添加内容页面中的脚本或代码(仅应用于此特定页面)

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

//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>

该部分需要在每个内容页面中提供,并且它将独占于该页面 - 其他页面不能使用该部分中的脚本

asp:Content ID =“HeaderContent”是一个内容区域。 该标记内的任何内容都将在生成时嵌入到母版页中的关联ContentPlaceHolder中。

head是标准的html标记,表示页头元素。 通常,HeadContent占位符位于母版页的head标记内。

头元素,所有头元素的容器,必须使用文档的标题。 它可以包括一些其他元素:样式,基础,链接,元,脚本,noscript。

asp:Content ID =“HeaderContent”是母版页的内容元素。 有关详细信息,请查看以下链接的插入内容部分: http//odetocode.com/articles/419.aspx

我想你问你什么时候想要使用JavaScript,把JS放在你的代码中。你可以把你想要的任何地方放在脚本块之间,如:

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

要么

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

你也可以把JS放在这个标签之外。 你只应该使用标签。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM