Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

ASP.NET HTML Output

Options
  • 05-03-2007 10:48pm
    #1
    Registered Users Posts: 15,065 ✭✭✭✭


    I am currently developing a site using ASP.NET as a learning exercise. The first issue that I have come across that has me well confused is dynamic output of HTML at specific areas of the screen. The site displays two different menus on the left hand side of the screen depending on the section of the site that the user is currently in. There are common pages available from both menus e.g. the index page, contact us or about us page. When any of the common pages are opened, the same menu as whichever page they came from should be displayed e.g. if they came from the commercial section, they would see the commercial menu or if they came from the residential section they would see the residential menu.

    Leaving aside the issue of the user coming from another page entirely, what is the most appropriate way to do this in ASP.NET? In classic ASP I have typically just done something like:
    <html>
    <body>
    <!--HTML Code -->
    <%
                'ASP code to output the menu dynamically.
    %>
    <!&#8212; More HTML Code -->
    </body>
    </html>
    
    The trouble is as far as I can see with ASP.NET, it’s event-driven so I could output all the HTML through the Page_Load() event and thereby change the menu but I’d rather keep the HTML separate from the code. I have done some reading on this and some sources seem to be suggesting that a static method in a separate utility class is the way to go. I can get this to work but it just puts the text at the top left of the window. Any suggestions or comments are greatly appreciated!


Comments

  • Registered Users Posts: 21,257 ✭✭✭✭Eoin


    I use C# on VS 2005, so not sure how relevant this is to you.

    Put a label (from the "standard" section of the Toolbox) in the area you wish to output the HTML to dynamically, and then in your code behind page, output the html to the control.

    e.g.
    [html]
    <tr>
    <td class="value" align="center" colspan="2">
    <asp:Label id="lblHTML" runat="server" CssClass="myClass"></asp:Label>
    </td>
    </tr>
    [/html]
    string sHTML = "<strong>markup goes here</strong>";
    lblHTML.Text = sHTML;
    


  • Registered Users Posts: 7,468 ✭✭✭Evil Phil


    If you're going to be formating the markup from code you could use (to follow the above example):
    lblHTML.Text = "label text goes here";
    lblHTML.Font.Bold = true;
    
    Personally I'd set the attributes in the markup so you could set the label's Font-Bold attribute to true to avoid doing it in code. Either way seems tidier to me rather than including markup in the output string. Would you not use CSS though?


  • Registered Users Posts: 21,257 ✭✭✭✭Eoin


    Evil Phil wrote:
    If you're going to be formating the markup from code you could use (to follow the above example):
    lblHTML.Text = "label text goes here";
    lblHTML.Font.Bold = true;
    
    Personally I'd set the attributes in the markup so you could set the label's Font-Bold attribute to true to avoid doing it in code. Either way seems tidier to me rather than including markup in the output string. Would you not use CSS though?

    You're dead right; I was just illustrating the markup within the string.


  • Registered Users Posts: 7,468 ✭✭✭Evil Phil


    Fair point :o

    I've heard the argument for generating pages with a lot of dynamic content (such as for large tables) that including the markup in that way does have a performance gain particularly if your using StringBuilders. I haven't seen the benchmarking myself so I dunno, it would have to be significant for me to use it though.


  • Registered Users Posts: 21,257 ✭✭✭✭Eoin


    Evil Phil wrote:
    Fair point :o

    I've heard the argument for generating pages with a lot of dynamic content (such as for large tables) that including the markup in that way does have a performance gain particularly if your using StringBuilders. I haven't seen the benchmarking myself so I dunno, it would have to be significant for me to use it though.

    If I was dealing with a large table - presumably lots of rows from a datasource - then maybe the Repeater control is probably the way to go, but for smaller bits of text, is there a decent alternative to labels? (I'm fairly new to .net 2005)


  • Advertisement
  • Registered Users Posts: 15,065 ✭✭✭✭Malice


    Eoin_s, thanks for that suggestion, I've just done a quick test with the label control. It seems to be doing just what I need. I will play around with it more tomorrow as I'm heading out the door to watch Liverpool v Barcelona soon tonight.

    For what it's worth, I'm using Visual Studio .NET 2003. Also the menu text is done through an unordered list in a div which has been defined in a separate style sheet. No need to mess with table rows, columns or cells!


  • Registered Users Posts: 21,257 ✭✭✭✭Eoin


    malice_ wrote:
    Eoin_s, thanks for that suggestion, I've just done a quick test with the label control. It seems to be doing just what I need. I will play around with it more tomorrow as I'm heading out the door to watch Liverpool v Barcelona soon tonight.

    For what it's worth, I'm using Visual Studio .NET 2003. Also the menu text is done through an unordered list in a div which has been defined in a separate style sheet. No need to mess with table rows, columns or cells!

    Great stuff, hope you get on OK. The placeholder control seems to be quite similar, but looks to be designed for larger chunks of content, where as a label seems to be like an inline element. A quick google should confirm one way or the other.


  • Moderators, Society & Culture Moderators Posts: 9,689 Mod ✭✭✭✭stevenmu


    There's a literal control in there too specifically for outputing raw html like this. In this case you would use it just like the label but it's more "correct" (I think)


  • Registered Users Posts: 21,257 ✭✭✭✭Eoin


    stevenmu wrote:
    There's a literal control in there too specifically for outputing raw html like this. In this case you would use it just like the label but it's more "correct" (I think)

    That sounds right. I think the main difference is that the literal control does not render a <span> tag around the content, so you can't apply a cssClass to it. This is probably fine for large lumps of HTML that have their own style declarations themselves.

    I was wrong about the placeholder - I don't think you can define the inner Contents of it directly, but you can add controls to it.


Advertisement