asp.net – 了解runat服务器属性

前端之家收集整理的这篇文章主要介绍了asp.net – 了解runat服务器属性前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我真的很新的ASP.NET。我只是检出一个默认的ASP.NET Web应用程序。它默认带有几页(Default.aspx,About.aspx等)。

我注意到Site.master文件是我为我的页面创建主布局的文件
但我也注意到head标签有一个runat =“server”属性

我知道这个标签用于< asp:XXX>标签,但是为什么在< head>标签???

此外,当我删除属性,那么所有的样式都从网页。所以显然它做了一些事情。我只是不明白它的确在做什么…

那么为什么它在那里,在一个HTML标签… ???我没有看到任何代码,应该在服务器上运行…

<head runat="server">
    <title>Hallo</title>
    <link href="~/Styles/Site.css" rel="stylesheet" type="text/css" />

    <!-- This part is run on the server. So why does the head tag
         also need a runat=server ?? -->
    <asp:ContentPlaceHolder ID="HeadContent" runat="server">
    </asp:ContentPlaceHolder>
</head>

解决方法

head元素包含一个runat =“server”属性,表示它是一个服务器控件(而不是静态HTML)。所有ASP.NET页面派生自Page类,它位于System.Web.UI命名空间中。这个类包含一个Header属性,提供对页面区域的访问。使用Header属性,我们可以设置一个ASP.NET页面标题添加额外的标记到呈现的部分。然后,可以通过在页面的Page_Load事件处理程序中写入一小段代码自定义内容页面的元素。
' Programmatically add a <Meta> element to the Header

Dim keywords As New HtmlMeta()
keywords.Name = "keywords"
keywords.Content = "master page,asp.net,tutorial"
Page.Header.Controls.Add(keywords)

http://www.asp.net/web-forms/tutorials/master-pages/specifying-the-title-meta-tags-and-other-html-headers-in-the-master-page-vb

原文链接:https://www.f2er.com/aspnet/254026.html

猜你在找的asp.Net相关文章