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);
开发者_StackOverflow LinkButton lb = new LinkButton();
lb.Text = fc.Title;
MenuPanel.Controls.Add(lb);
// I want some sort of line break here
}
}
Use the LiteralControl
class to insert a line break...
MenuPanel.Controls.Add(new LiteralControl("<br />"));
Or use CSS to make your links block-level elements...
#menu a { display: block; }
FYI : If you have already added the panel control in the aspx(design-view) and if you want to use the above accepted answer in .cs file (code-behind)., then you will be running into type errors. So in that case, you could use this way. Please note the small cased "new".
Panel1.Controls.Add(new LiteralControl("<br>"));
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.
You could do this:
HtmlGenericControl div = new HtmlGenericControl("div");
div.Text = " ";
MenuPanel.Controls.Add(div);
in the panel control u can use lable control which have " value in text property
Label lb1 = new Label();
lb1.Text = "<br>";
Panel1.Controls.Add(lb1);
精彩评论