简体   繁体   English

从代码后面的HTML

[英]html from code behind

我想从(asp.net c#)后面的代码中插入带有几个控件+ style的html怎么办?

You could use a <asp:PlaceHolder> then add controls to this. 您可以使用<asp:PlaceHolder>然后向其中添加控件。

eg 例如

Image img = new Image();
img.ImageUrl = "/someurl.jpg";
img.CssClass = "someclass";
img.ID = "someid";
img.AlternateText = "alttext"

PlageHolderId.Controls.Add(img);

This would produce the html 这将产生HTML

<img src="/someurl.jpg" class="someclass" id="someid" alt="alttext" />

You can then do this will any control, literal, hyperlink, button, table, etc... 然后,您可以执行所有控件,文字,超链接,按钮,表格等操作...

You can add <asp:Literal> controls in the markup, then set their Text s in code-behind. 您可以在标记中添加<asp:Literal>控件,然后在代码隐藏中设置其Text
Make sure to set Mode="PassThrough" to prevent them from escaping the HTML. 确保设置Mode="PassThrough"以防止它们转义HTML。

You can add server-side controls by adding them to the Controls collection of any existing control (such as an <asp:Panel> ) 您可以通过将服务器端控件添加到任何现有控件(例如<asp:Panel> )的Controls集合中来添加服务器端控件

Instead of Literal control you can use either HtmlGenericControl 可以使用HtmlGenericControl来代替文字控制

HtmlGenericControl div = new HtmlGenericControl();
div.ID = "div";
div.TagName = "div";
div.Attributes["class"] = "container";
form1.Controls.Add(div);

Add a few <asp:PlaceHolder> 's to your template file in the <head> and <body> <head><body>中的模板文件中添加一些<asp:PlaceHolder>

Then use PlaceHolder1.Controls.Add(); 然后使用PlaceHolder1.Controls.Add();

I put a <asp:Panel ID="myPanel" runat="server"/> , and in the codebehind i add controls with: 我放入<asp:Panel ID="myPanel" runat="server"/> ,并在<asp:Panel ID="myPanel" runat="server"/>的代码中添加以下控件:

myPanel.Controls.Add(...)

If you want to insert HTML code direct to the panel, use 如果要直接将HTML代码插入面板,请使用

myPanel.Controls.Add(new LiteralControl("Your HTML goes here!"))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM