Home > .net 2.0, c#, Visual Studio 2005 > Adding Line Breaks Between Controls in a PlaceHolder

Adding Line Breaks Between Controls in a PlaceHolder

June 4, 2007

Okay, so I’ve done this manually, but decided to refactor it up into a little method and add it to our Utility class of our framework… because it’s so inane, but so common, that.. that.. *codes self in a corner*

When adding controls to a Placeholder, there isn’t a simple way to simply insert a break between controls.  For example, if you’re adding a list of links or paragraphs, you must rely on padding the control with a <br/> or <p> set to ensure padding.  Well, that’s not always the case, especially for buttons.

So…

    private static Label InsertLineBreaks(int breaks)

    {

        Label lblLineBreak = new Label();

        for (int i = 0; i<breaks; i++)

        {

            lblLineBreak.Text += “<br/>”;

        }

        return lblLineBreak;

    }

Yes, it’s crude, yes, it’s simple… but by gosh, it saves me from retyping it.

It’s use?

phSection1Content.Controls.Add(InsertLineBreaks(2));

It adds a break for each number you provide to it in it’s constructor.

  1. July 9, 2007 at 4:48 pm

    Slick! When dynamically adding controls I even go so far as to append Environment.NewLine so that my HTML looks perfectly manicured.

    I’m definitely going to remember this little trick for future use.

  2. July 9, 2007 at 6:02 pm

    Ahh, good call! I use that for my console apps, but never really dinked with it on webs; it makes the code look great. Thanks!

  3. July 9, 2007 at 7:04 pm

    No problem😉

  4. Mir
    September 21, 2008 at 4:42 am

    PlaceHolder1.Controls.Add(new LiteralControl(""));

  5. Mir
    September 21, 2008 at 4:45 am

    oh, for the code above, there should be new paragraph html code between the quote signs in there, i think the system has deleted it…

    PlaceHolder1.Controls.Add(new LiteralControl(" "));

  1. No trackbacks yet.
Comments are closed.
%d bloggers like this: