Erik Porter's Blog

Life and Development at Microsoft and Other Technology Discussions

News

    Quick ASP.NET Tip

    We've all got time for a quickie, right?  Well here you go.  Everyone and their mom (or not) may already do this already, but...

    Whenever I have some text to display on a page and the text is dynamic, my first thought is to just dump it out:

    <b>Name:</b> <asp:literal id="litName" runat="server"></asp:literal>

    Back in your code, you then say:

    litName.Text = User.FirstName & " " & User.LastName

    or something like that.  Typically though, I actually do it like this:

    <asp:literal id="litName" runat="server"><b>Name:</b> {0} {1}</asp:literal>

    Then the code of the page is like this:

    litName.Text = String.Format(litName.Text, User.FirstName, User.LastName)

    Main reason I do this is to plan for change.  If the designer wants to change something like say show LastName, FirstName just change the format and s/he's done.  No recompiling, just save and run and done.  You might ask why I included the header in there.  Mostly just because it makes me feel better.  Keeping all related text in the same literal.  In the first example, I as the developer would've had to make the change for the design in code, which IMO, sucks!  By having as much as possible in the ASPX related to formatting, a designer can separately make changes in their editor of choice and test it without having to ask me for new dll's.

    Posted: Apr 25 2005, 07:19 PM by HumanCompiler | with 3 comment(s)
    Filed under: ,

    Comments

    Paul Wilson said:

    Very cool tip.
    # April 25, 2005 11:52 PM

    Brett said:

    Agreed, I do this too. I try to allow my designer (whether they use Dreamweaver, Frontpage etc.) to change as much as I can allow (within reason) easily without having to get me to recompile.
    # April 26, 2005 12:54 AM

    TrackBack said:

    RossCode.com - Formatting Strings In ASP.NET
    # September 18, 2005 3:32 AM