Page.Header.Controls.Add question (anyway to make output pretty?)

Last post 01-30-2009 3:59 AM by agthr. 2 replies.

Sort Posts:

  • Page.Header.Controls.Add question (anyway to make output pretty?)

    01-29-2009, 11:53 PM
    • Member
      67 point Member
    • agthr
    • Member since 06-24-2002, 3:24 AM
    • Posts 30

    Hello.
    I think ASP.NET has many many great fetures and is indeed very productive, but I must say outputs are not very pretty (ASP.NET HTML that is)

    Say I want to add something to the header section of my page, maybe 2 links to CSS and a JavaScript.

     

    //My first CSS
    HtmlLink myCSS = new HtmlLink();
    myCSS.Href = "~/somecss1.css";
    myCSS.Attributes.Add("rel", "stylesheet");
    myCSS.Attributes.Add("type", "text/css");
    Page.Header.Controls.Add(myCSS);
    
    //Here comes my second
    myCSS = new HtmlLink();
    myCSS.Href = "~/somecss2.css";
    myCSS.Attributes.Add("rel", "stylesheet");
    myCSS.Attributes.Add("type", "text/css");
    Page.Header.Controls.Add(myCSS);
    
    //JavaScript!
    HtmlGenericControl myJavaScript = new HtmlGenericControl();
    myJavaScript.TagName = "script";
    myJavaScript.Attributes.Add("type", "text/javascript");
    myJavaScript.InnerText = "alert('Hello');";
    Page.Header.Controls.Add(myJavaScript);
    

     

    It's all easy and fancy, but when I look at the output HTML, its all in oneline!

    <link href="somecss1.css" rel="stylesheet" type="text/css" /><link href="somecss2.css" rel="stylesheet" type="text/css" /><script type="text/javascript">alert('Hello');</script>

     Are there ANYWAY to make it so the output is like

    <link href="somecss1.css" rel="stylesheet" type="text/css" />
    <link href="somecss2.css" rel="stylesheet" type="text/css" />
    <script type="text/javascript">
    alert('Hello');
    </script>
    Or its just the way it is? Thanks
    _/_/_/_/_/_/_/_/_/_/_/
  • Re: Page.Header.Controls.Add question (anyway to make output pretty?)

    01-30-2009, 3:42 AM
    Answer
    • Contributor
      2,411 point Contributor
    • Rick Matthys
    • Member since 01-20-2009, 3:57 PM
    • Oregon
    • Posts 347

    I'm not sure what the logic is behind the covers.   I tried a few examples to see if I could identify the pattern.  If you dynamically add texboxes, labels, etc.. you also see them in the same line while viewing source.  Tables, rows, and cells end up with line breaks, as do divs and others.  Maybe it's just containers, who knows...

    I think you're right in that you can't control it with out extra effort.  If you had some reason you really wanted your html formatted, you could always add a literal control.  for example:

    //My first CSS
    HtmlLink myCSS = new HtmlLink();
    myCSS.Href = "~/somecss1.css";
    myCSS.Attributes.Add("rel", "stylesheet");
    myCSS.Attributes.Add("type", "text/css");
    Page.Header.Controls.Add(myCSS);

    Page.Header.Controls.Add(new LiteralControl("\r\n"));

    //Here comes my second
    myCSS = new HtmlLink();
    myCSS.Href = "~/somecss2.css";
    myCSS.Attributes.Add("rel", "stylesheet");
    myCSS.Attributes.Add("type", "text/css");
    Page.Header.Controls.Add(myCSS);

    Page.Header.Controls.Add(new LiteralControl("\r\n"));

    ~Rick

    Please mark the post as ANSWER if it helps you

    Disclaimer: Just my opinion. Not my employer or anyone else....
  • Re: Page.Header.Controls.Add question (anyway to make output pretty?)

    01-30-2009, 3:59 AM
    • Member
      67 point Member
    • agthr
    • Member since 06-24-2002, 3:24 AM
    • Posts 30

    Thank you for your reply!

    Rick Matthys:
    Page.Header.Controls.Add(new LiteralControl("\r\n"));

    This would work just fine! Thank you so much.

    _/_/_/_/_/_/_/_/_/_/_/
Page 1 of 1 (3 items)