C# 如何在面板内添加换行符或 html?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2118316/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 23:39:52  来源:igfitidea点击:

How can I add a line break or html inside of a Panel?

c#asp.net.netfunctionforeach

提问by ProgrammingPope

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

采纳答案by Josh Stodola

Use the LiteralControlclass 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; }

回答by hunter

You could do this:

你可以这样做:

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

回答by Chris Simpson

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 元素可能是值得的。这意味着您不必使用可怕的 br 标签或任何黑客来获得您需要的东西。

回答by Shankar Damodaran

FYI : If you have already added the panel controlin 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(design-view) 中添加了面板控件,并且如果您想在 .cs 文件(代码隐藏)中使用上面接受的答案,那么您将遇到类型错误。所以在这种情况下,你可以使用这种方式。请注意小包装的“新”。

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

回答by manish

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