如何在后面的c#代码中将div添加到容器div
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1165529/
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
how to add a div to container div in c# code behind
提问by ErnieStings
ASP.NET, C#
ASP.NET, C#
As the title suggests I was wondering if anyone knew how to programatically (c# code behind file) add a div to another a container div (in the aspx page).
正如标题所示,我想知道是否有人知道如何以编程方式(c# 文件隐藏代码)将一个 div 添加到另一个容器 div(在 aspx 页面中)。
Thanks in advance
提前致谢
回答by Otávio Décio
Use asp:Panel, which maps to a div.
使用 asp:Panel,它映射到一个 div。
回答by Matthew Vines
Panel myChildPanel = new Panel();
myContainerPanel.Controls.Add(myChildPanel);
回答by Thorarin
Aside from using a Panel like ocdecio suggested, there are several other possibilities.
除了使用像 ocdecio 建议的面板之外,还有其他几种可能性。
- You can use an asp:Literal control inside the div and fill that with pregenerated HTML
- Add a runat="server" to the div itself and access it as a HtmlGenericControl, adding other controls to it from your codebehind.
- Using <%= ... %>
- 您可以在 div 中使用 asp:Literal 控件并用预生成的 HTML 填充它
- 将 runat="server" 添加到 div 本身并将其作为 HtmlGenericControl 访问,从代码隐藏向其添加其他控件。
- 使用 <%= ... %>
It depends a little on the level of control you need. Still, under a most circumstances, a Panel that starts out invisible would be best:
这在一定程度上取决于您需要的控制级别。尽管如此,在大多数情况下,一开始不可见的面板会是最好的:
<div>
<asp:Panel Visible="false" id="MyPanel" runat="server">
</asp:Panel>
</div>
Then change the visibility from your codebehind when needed.
然后在需要时从代码隐藏更改可见性。
One cases where you might want to use one of the other methods is when you're stuck with some CSS file that assigns styles based on ID. In that case, using .NET controls is not really an option. But really, you should smack your designer over the head and tell him to use class names instead.
您可能想要使用其他方法之一的一种情况是,当您遇到一些基于 ID 分配样式的 CSS 文件时。在这种情况下,使用 .NET 控件并不是一个真正的选择。但实际上,你应该抨击你的设计师并告诉他改用类名。
回答by Jacob O'Brien
//create new instance of div and set all the values like the ID Check out the short Code example. It worked for me to create Divs in a web add
//创建 div 的新实例并设置所有值,如 ID 查看简短的代码示例。在网络添加中创建 Div 对我有用
System.Web.UI.HtmlControls.HtmlGenericControl NewDiv = new
System.Web.UI.HtmlControls.HtmlGenericControl();
NewDiv.ID = "divcreated";
or
或者
protected void Page_Load(object sender, EventArgs e)
{
System.Web.UI.HtmlControls.HtmlGenericControl createDiv =
new System.Web.UI.HtmlControls.HtmlGenericControl("DIV");
createDiv.ID = "createDiv";
createDiv.Style.Add(HtmlTextWriterStyle.BackgroundColor, "Yellow");
createDiv.Style.Add(HtmlTextWriterStyle.Color, "Red");
createDiv.Style.Add(HtmlTextWriterStyle.Height, "100px");
createDiv.Style.Add(HtmlTextWriterStyle.Width, "400px");
createDiv.InnerHtml = " I'm a div, from code behind ";
this.Controls.Add(createDiv);
}
回答by JCO9
This may be a very old question but I would like to add my solution for helping:
这可能是一个非常古老的问题,但我想添加我的解决方案以提供帮助:
First, to the "div" you already have in your page (the one you want to add another "div" to) give the runat="server" property so you can access it from code behind, it would look like this:
首先,对页面中已有的“div”(要添加另一个“div”的那个)赋予 runat="server" 属性,以便您可以从后面的代码访问它,它看起来像这样:
<div id="superDIV" class="someCssClass" runat="server"></div>
Then in your Page_Load() method add the following:
然后在您的 Page_Load() 方法中添加以下内容:
protected void Page_Load(object sender, EventArgs e)
{
//We create our new div
System.Web.UI.HtmlControls.HtmlGenericControl newDiv =
new System.Web.UI.HtmlControls.HtmlGenericControl("DIV");
newDiv.ID = "newSuperDIV"; //<---Give and ID to the div, very important!
newDiv.Style.Value = "background-color:white; height:61%;"; //<---Add some style as example
newDiv.Attributes.Add("class", "amazingCssClass"); //<---Apply a css class if wanted
superDiv.Controls.Add(newDiv); //<---Add the new div to our already existing div
}
Genearte your div directly inside the Page_Loadfunction so it will assure that exists after any postback, avoid generating it inside code blocks like (!IsPostBack){}otherwise it will not exist in your page.
直接在Page_Load函数内生成您的 div,以便确保在任何回发后都存在,避免在(!IsPostBack){}等代码块内生成它,否则它将不存在于您的页面中。