ASP.NET
Topic: CodeSnip: Setting Commonly-Used Tags Programmatically

Ireland .NET Developer Alliance :: Forums

Page 1 of 1 (1 items)


Posted by Paschal on

Very useful tip found in ASP Alliance . [:D]

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.

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.