开发者

how to remove html comment tag before render in asp.net

开发者 https://www.devze.com 2022-12-16 23:08 出处:网络
i\'m using some components. When page render, components generate html comment tags. if i\'m using th开发者_高级运维is component 10 times on one page, html comments inserted 10 times.

i'm using some components. When page render, components generate html comment tags. if i'm using th开发者_高级运维is component 10 times on one page, html comments inserted 10 times.

How to remove html comment tag before render page?


Use server side comments:

<%--
    Commented out HTML/CODE/Markup.  Anything with
    this block will not be parsed/handled by ASP.NET.

    <asp:Calendar runat="server"></asp:Calendar> 

   <%# Eval(“SomeProperty”) %>     
--%>

or only render the comment in Debug mode

#if DEBUG
// Add my comment for debug only
#endif


Create a custom server control based on the 3rd party assembly, like so:

namespace ServerControls
{
    [ToolboxData("<{0}:LabelWithComment runat=server></{0}:LabelWithComment>")]
    public class LabelWithComment : Label
    {
        protected override void Render(HtmlTextWriter output)
        {
            var htmlFromBaseClass = new StringBuilder();
            var htmlTextWriterForBaseClass = 
                new HtmlTextWriter(new StringWriter(htmlFromBaseClass));
            base.Render(htmlTextWriterForBaseClass);
            var modifiedHtml = ModifyHtmlUsing(htmlFromBaseClass);
            output.Write(modifiedHtml);
        }

        private static string ModifyHtmlUsing(StringBuilder stringBuilder)
        {
            stringBuilder.Replace("<!-- some comment -->", "");
            return stringBuilder.ToString();
        }
    }
}

Then customize the ModifyHtmlUsing method to repalce whatever you want.

Then include this directive on the page where you are using the control:

<%@ Register Assembly="ServerControls" Namespace="ServerControls" TagPrefix="Custom"  %>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <Custom:LabelWithComment ID="lblLabelWithComments" 
        Text="Some Text <!-- some comment -->" runat="server" />
    </div>
    </form>
</body>
</html>


What kind of component? Let's say its a custom server control that renders the HTML comments. Each server control controls its process for rendering content to the browser. So HTML comments would be rendered by this control and render it directly to the browser. As an example, it would be the following: http://msdn.microsoft.com/en-us/library/aa338806%28VS.71%29.aspx You could create your own class that inherits from this component and change the rendering process, but that is inefficient.

You could use javascript to potentially do this, but I have to ask why are these comments an issue? THey may not be rendered to the browser when your application is built in release mode...

HTH.

0

精彩评论

暂无评论...
验证码 换一张
取 消