在 C# 中创建 html div
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15746644/
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
creating html div in c#
提问by Hanya Idrees
I am using the following code to make an html div in c#
我正在使用以下代码在 c# 中创建一个 html div
System.Web.UI.HtmlControls.HtmlGenericControl dynDiv =
new System.Web.UI.HtmlControls.HtmlGenericControl("DIV");
dynDiv.ID = "dynDivCode";
dynDiv.Style.Add(HtmlTextWriterStyle.BackgroundColor, "Gray");
dynDiv.Style.Add(HtmlTextWriterStyle.Height, "20px");
dynDiv.Style.Add(HtmlTextWriterStyle.Width, "300px");
dynDiv.InnerHtml = "I was created using Code Behind";
this.Controls.Add(dynDiv);
But this doesnot do a thing, infact it gives an error at the last line that dynDiv is not a valid argument. I want to use div here to simulate cache memory line and placement of words in cache memory.Please tell me how to do it
但这并没有做任何事情,事实上它在最后一行给出了一个错误,即 dynDiv 不是一个有效的参数。我想在这里用div来模拟缓存行和单词在缓存中的放置。请告诉我怎么做
回答by Severiano
use LITERAL CONTROL
使用文字控制
HTML(example)
HTML(示例)
<asp:Panel ID="panel" runat="server"> </asp:Panel>
C#
C#
//And create the style as you want
//并根据需要创建样式
panel.Controls.Add(new LiteralControl("<div style='color: gray; height: 20px; width: 300px;'>I was created using Code Behind</div>"));
回答by scartag
You could just embed the html inside a literal control.
您可以将 html 嵌入到文字控件中。
this.Controls.Add(new LiteralControl("<div style='color: gray; height: 20px; width: 300px;'>I was created using Code Behind</div>"));
回答by Severiano
maybe this helps: Adding panels to SplitContainer in Windows Forms
也许这会有所帮助: 在 Windows 窗体中向 SplitContainer 添加面板
Panel panel = new Panel();
Label lbl = new Label();
public Form1()
{
InitializeComponent();
panel.BackColor = Color.Gray;
panel.Height = 20;
panel.Width = 300;
lbl.Text = "I was created using Code Behind";
panel.Controls.Add(lbl);
dynDiv.Panel1.Controls.Add(panel);
}
for what you say this is what you want
因为你说的这就是你想要的
回答by Ioannes Carolvs
I made as Diogo Severianosaid and it worked.
我按照Diogo Severiano说的做了,而且效果很好。
In my case I needed to add controls to a div and I could not use a literal control because intellisense marks: "controls cannot be added to a literal control".
在我的情况下,我需要将控件添加到 div 并且我无法使用文字控件,因为智能感知标记:“控件不能添加到文字控件”。
If that is the case do as he did.
如果是这样的话,就照他做的那样做。
Label lblParaname = new Label();
lblParaname.Text = Record.Parameter_Name + ": ";
lblParaname.ID = "lbl" + Record.Parameter_Name;
lblParaname.EnableViewState = true;
lblParaname.Attributes.Add("cssclass", "grid_3 alpha omega");
Panel pLblContainer = new Panel();
pLblContainer.CssClass = "grid_3";
pLblContainer.Controls.Add(lblParaname);
pSubsPar.Controls.Add(pLblContainer);
Label lblParaname = new Label();
lblParaname.Text = Record.Parameter_Name + ": ";
lblParaname.ID = "lbl" + Record.Parameter_Name;
lblParaname.EnableViewState = true;
lblParaname.Attributes.Add("cssclass", "grid_3 alpha omega");
Panel pLblContainer = new Panel();
pLblContainer.CssClass = "grid_3";
pLblContainer.Controls.Add(lblParaname);
pSubsPar.Controls.Add(pLblContainer);