ArticleDirectory
Allow programming to access the HTML <link> element on the server.
The followingCodeThe example shows how to declare the htmllink control programmatically and define its properties.
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 Head1.controls. Add (myhtmllink );
}
}
Htmllink. href attributes
The href attribute specifies the URL target for the link specified in the htmllink control. You can use this attribute to specify the position of an external Cascading Style Sheet (CSS.
The following code example shows how to set the href attribute to save Cascading Style Sheets (CSS) in the directory where the webpage is located ).
Htmlcontrol. Attributes attributes
Obtains the set of all attribute names and value pairs represented on the server control tag on the ASP. NET page.
C #
Public attributecollection attributes {Get ;}
Attribute Value
System. Web. UI. attributecollection object, which contains all attribute names and value pairs marked by server controls on the webpage.
You can use this property to access the attribute of the HTML Server Control programmatically ). All HTML server controls store their attributes in control. viewstate attributes.
The HTML attribute is treated as a property on the HTML server control to which the. NET Framework belongs ).
The following code example demonstrates how to use the attributes attribute to determine the attribute of the htmlselect control ).
<% @ 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
Make a selection:
<Select id = "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 id = "message" maintainstate = "false" runat = "server"/>
</Body>
</Html>