ASP.NET自学--HtmlLink

允许编程访问服务器上的 HTML <link> 元素。

下面的代码示例演示如何以编程方式声明 HtmlLink 控件并定义它的属性。

using System;

using System.Data;

using System.Configuration;

using System.Web;

using System.Web.Security;

using System.Web.UI;

using System.Web.UI.WebControls;

using System.Web.UI.WebControls.WebParts;

using System.Web.UI.HtmlControls;

public partial class HtmlLinkcs_aspx

{

void Page_Init(object sender, EventArgs e)

{

// Create an instance of HtmlLink.

HtmlLink myHtmlLink = new HtmlLink();

myHtmlLink.Href = "StyleSheet.css";

myHtmlLink.Attributes.Add("rel", "stylesheet");

myHtmlLink.Attributes.Add("type", "text/css");

// Add the instance of HtmlLink to the <HEAD> section of the page.

head1.Controls.Add(myHtmlLink);

}

}

HtmlLink.Href 属性

Href 属性为 HtmlLink 控件中指定的链接指定 URL 目标。可以使用此属性指定外部联级样式表 (CSS) 的位置。

下面的代码示例演示如何将 Href 属性设置为保存在网页所在的目录中的级联样式表 (CSS)。

HtmlControl.Attributes 属性

获取在 ASP.NET 页内的服务器控件标记上表示的所有属性 (Attribute) 名称和值对的集合。

C#

public AttributeCollection Attributes { get; }

属性值

System.Web.UI.AttributeCollection 对象,它包含网页内的服务器控件标记上表示的所有属性 (Attribute) 名称和值对。

使用该属性 (Property) 可以用编程方式访问 HTML 服务器控件的属性 (Attribute)。所有 HTML 服务器控件都将其属性 (Attribute) 存储在 Control.ViewState 属性 (Property) 中。

HTML 属性 (Attribute) 被 .NET Framework 视为它们所属的 HTML 服务器控件上的属性 (Property)。

下面的代码示例演示如何使用 Attributes 属性 (Property) 确定 HtmlSelect 控件的属性 (Attribute)。

<%@ Page Language="C#" AutoEventWireup="True" %>

<html>

<script language="C#" runat="server">

void Page_Load(Object sender, EventArgs e)

{

Message.InnerHtml = "<h4>The select box's attributes collection contains:</h4>";

IEnumerator keys = Select.Attributes.Keys.GetEnumerator();

while (keys.MoveNext())

{

String key = (String)keys.Current;

Message.InnerHtml += key + "=" + Select.Attributes[key] + "<br>";

}

}

</script>

<body>

<h3>HtmlControl Attribute Collection Example</h3>

Make a selection:

<select

style="font: 12pt verdana;

background-color:yellow;

color:red;"

runat="server">

<option>Item 1</option>

<option>Item 2</option>

<option>Item 3</option>

</select>

<p>

<span />

</body>

</html>