CodeSnip: Setting Commonly-Used Tags Programmatically
page 1 of 1
Published: 29 Oct 2004
Unedited - Community Contributed
Abstract
The runat attribute can be applied to the meta and stylesheet tags, just to name a couple. In HTML, you normally add a meta tag without a closing tag, however in ASP.NET, when the runat attribute is added, you must add the closing tag. The ASP.NET parser scans the file looking for closing a meta tag; if the tag is not found, a compiler error is generated. This can be very useful if you have a master page template or a web control such as a header that will be applied to every web page.
by Steven Swafford
Feedback
Average Rating: 
Views (Total / Last 10 Days): 8392/ 13

The following code shows how to programmatically set meta content tag that instructs the client to wait ten seconds and then to go to the specified URL.  Also, it will render the stylesheet tag attribute in this same method.

<%@ Page Language="VB" %>
<script runat="server">
Sub Page_Load(sender As Object, e As EventArgs)
    aspNet.Attributes("content") = "10;URL="
    StyleSheet.Attributes["href"] = "style.css"
End Sub
</script>

<html>
<meta id="aspNet" runat="server" http-equiv="refresh"></meta>
<link id="StyleSheet" rel="stylesheet" type="text/css" runat="server"></link>
<body>
Content
</body>
</html>

<%@ Page Language="C#" %>
<script runat="server">
void Page_Load(object sender, EventArgs e)
{
    aspNet.Attributes["content"] = @"10;URL=";
    StyleSheet.Attributes["href"] = "style.css";
}
</script>

<html>
<meta id="aspNet" runat="server" http-equiv="refresh"></meta>
<link id="StyleSheet" rel="stylesheet" type="text/css" runat="server"></link>
<body>
Content
</body>
</html>

The resulting HTML tags are shown below.

<meta id="aspNet" http-equiv="refresh" content="4;URL="></meta>
<link id="StyleSheet" rel="stylesheet" type="text/css" runat="server" href="style.css"></link>

Notice the very important presence of the closing tag in the instance of the meta tag and stylesheet link tags.

You could even extend this functionality even further by creating and compiling a utility class that has all your common code such as page attributes, email addresses, and etc. This way, when a change is required, this one single class is the only piece of the application that will require changes in the case of changing an email address and any page stylesheet references as I used in my example above. This snippet should, however, give you an idea on how to consolidate common tags and attributes used within an application.



User Comments

No comments posted yet.






Community Advice: ASP | SQL | XML | Regular Expressions | Windows


©Copyright 1998-2024 ASPAlliance.com  |  Page Processed at 2024-03-28 11:25:54 AM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search