如何在Panel中添加换行符或html?

时间:2022-04-16 03:05:04

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.

我正在尝试使用以下代码创建菜单。但我无法弄清楚如何让每个LinkBut​​ton出现在单独的行上。

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
  }
}

5 个解决方案

#1


16  

Use the LiteralControl class to insert a line break...

使用LiteralControl类插入换行符...

MenuPanel.Controls.Add(new LiteralControl("<br />")); 

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

或者使用CSS来创建链接块级元素......

#menu a { display: block; }

#2


2  

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.

我知道这个答案已被接受,但我想提出一个不同的选择。如果你想要一个垂直的元素列表,那么使用ul或ol元素可能是值得的。这意味着您不必使用可怕的标签或任何黑客来获得您需要的东西。

#3


2  

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".

仅供参考:如果您已经在aspx(设计视图)中添加了面板控件,并且想要在.cs文件(代码隐藏)中使用上面接受的答案,那么您将遇到类型错误。那么在这种情况下,你可以用这种方式。请注意小套装“新”。

Panel1.Controls.Add(new LiteralControl("<br>"));

#4


1  

You could do this:

你可以这样做:

HtmlGenericControl div = new HtmlGenericControl("div");
div.Text = "&nbsp;";
MenuPanel.Controls.Add(div);

#5


0  

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);     

#1


16  

Use the LiteralControl class to insert a line break...

使用LiteralControl类插入换行符...

MenuPanel.Controls.Add(new LiteralControl("<br />")); 

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

或者使用CSS来创建链接块级元素......

#menu a { display: block; }

#2


2  

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.

我知道这个答案已被接受,但我想提出一个不同的选择。如果你想要一个垂直的元素列表,那么使用ul或ol元素可能是值得的。这意味着您不必使用可怕的标签或任何黑客来获得您需要的东西。

#3


2  

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".

仅供参考:如果您已经在aspx(设计视图)中添加了面板控件,并且想要在.cs文件(代码隐藏)中使用上面接受的答案,那么您将遇到类型错误。那么在这种情况下,你可以用这种方式。请注意小套装“新”。

Panel1.Controls.Add(new LiteralControl("<br>"));

#4


1  

You could do this:

你可以这样做:

HtmlGenericControl div = new HtmlGenericControl("div");
div.Text = "&nbsp;";
MenuPanel.Controls.Add(div);

#5


0  

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);