开发者

html from code behind

I want to inse开发者_Go百科rt html with a few controls +style from the code behind ( asp.net c#) how can I do it ?


You could use a <asp:PlaceHolder> then add controls to this.

e.g.

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

<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 Texts in code-behind.
Make sure to set Mode="PassThrough" to prevent them from escaping the HTML.

You can add server-side controls by adding them to the Controls collection of any existing control (such as an <asp:Panel>)


Add a few <asp:PlaceHolder>'s to your template file in the <head> and <body>

Then use PlaceHolder1.Controls.Add();


I put a <asp:Panel ID="myPanel" runat="server"/> , and in the codebehind i add controls with:

myPanel.Controls.Add(...)

If you want to insert HTML code direct to the panel, use

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


Instead of Literal control you can use either HtmlGenericControl

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜