tags:

views:

117

answers:

3

I am trying to create a menu with the following code. But I cannot figure out how to get each LinkButton to appear on seperate lines.

MenuPanel.Controls.Clear();
foreach (FormList f in forms)
{
  if (f.IsActive == "y")
  {
     FormUserControl fc = (FormUserControl)LoadControl(f.StartControl);
     LinkButton lb = new LinkButton();
     lb.Text = fc.Title;
     MenuPanel.Controls.Add(lb);
     // I want some sort of line break here
  }
}
+3  A: 

Use the Literal control to insert a line break...

MenuPanel.Controls.Add(New Literal("<br />")); 

Or use CSS to make your links block-level elements...

#menu a { display: block; }
Josh Stodola
I had to use a LiteralControl, but essentially this worked great.
ProgrammingPope
+1 for the brace of good answers. Remember that you need to separate your links with more than white-space to comply with the WCAG specs (i.e. the first suggestion is better in this respect than the second).
Sohnee
A: 

You could do this:

HtmlGenericControl div = new HtmlGenericControl("div");
div.Text = "&nbsp;";
MenuPanel.Controls.Add(div);
hunter
+2  A: 

I know this answer has already been accepted but I'd like to suggest a different option. If you want a vertical list of elements then it might be worth using a ul or ol element. This means you don't have to use the dreaded br tag or any hacks to get what you need.

Chris Simpson
+1 for using markup for what it's designed for
Matt Ellen