How to Use RadControls for ASP.NET AJAX Embedded Skins with a HTML DOCTYPE

Thread is closed for posting
1 posts, 1 answers
  1. Answer
    63F75A2C-1F16-4AED-AFE8-B1BBD57646AD
    63F75A2C-1F16-4AED-AFE8-B1BBD57646AD avatar
    1572 posts
    Member since:
    Oct 2004

    Posted 21 Aug 2008 Link to this post

    Requirements

    RadControls version RadControls for ASP.NET AJAX
    .NET version 2.0
    Visual Studio version
    programming language C#, VB
    browser support

    all browsers supported by RadControls


     
    PROJECT DESCRIPTION

    By default, ASP.NET produces XHTML markup, which means that <link> tags (added programmatically as HtmlLink elements) are always self-closing. This is not valid if you are using an HTML DOCTYPE. In order to make your markup validate and still continue using automatically registered embedded skins, you need to replace the self-closing <link> tags with open tags:

    C#

    protected void Page_PreRenderComplete(object sender, EventArgs e) 
        List<HtmlLink> oldSkins = new List<HtmlLink>(); 
        List<LiteralControl> newSkins = new List<LiteralControl>(); 
        foreach (Control c in Page.Header.Controls) 
        { 
            if (c is HtmlLink && (c as HtmlLink).Attributes["class"] == "Telerik_stylesheet"
            { 
                LiteralControl lc = new LiteralControl(String.Format("<link href=\"{0}\" type=\"text/css\" rel=\"stylesheet\" class=\"Telerik_stylesheet\">", (c as HtmlLink).Href.Replace("&t""&amp;t"))); 
                oldSkins.Add(c as HtmlLink); 
                newSkins.Add(lc); 
            } 
        } 
     
        foreach (HtmlLink oldSkin in oldSkins) 
        { 
            Page.Header.Controls.Remove(oldSkin); 
        } 
     
        foreach (LiteralControl newSkin in newSkins) 
        { 
            Page.Header.Controls.Add(newSkin); 
        } 


    VB

        Protected Sub Page_PreRenderComplete(ByVal sender As ObjectByVal e As EventArgs) Handles Me.PreRenderComplete 
     
            Dim oldSkins As New List(Of HtmlLink)() 
            Dim newSkins As New List(Of LiteralControl)() 
     
            For Each c As Control In Me.Header.Controls 
                If TypeOf c Is HtmlLink AndAlso TryCast(c, HtmlLink).Attributes("class") = "Telerik_stylesheet" Then 
                    Dim lc As New LiteralControl([String].Format("<link href='{0}' type='text/css' rel='stylesheet' class='Telerik_stylesheet'>", (TryCast(c, HtmlLink)).Href.Replace("&t""&amp;t"))) 
                    oldSkins.Add(c) 
                    newSkins.Add(lc) 
                End If 
            Next 
     
            For Each oldSkin As HtmlLink In oldSkins 
                Me.Header.Controls.Remove(oldSkin) 
            Next 
     
            For Each newSkin As LiteralControl In newSkins 
                Me.Header.Controls.Add(newSkin) 
            Next 
     
        End Sub 


    By the way, in order to make ASP.NET render more HTML-compliant markup, you can set in web.config:

    <system.web> 
        <xhtmlConformance mode="Legacy" /> 
    </system.web> 
     

    However, you should be aware that this creates problems with ASP.NET AJAX:

    Gotcha: Don't use xhtmlConformance mode="Legacy" with ASP.NET AJAX


    For more information about ASP.NET 2.0 and web standards, please visit:

    MSDN: Building ASP.NET 2.0 Web Sites Using Web Standards



Back to Top

This Code Library is part of the product documentation and subject to the respective product license agreement.