mnaftal
mnaftal

Reputation: 51

html from code behind

I want to insert html with a few controls +style from the code behind ( asp.net c#) how can I do it ?

Upvotes: 2

Views: 12612

Answers (5)

user2724280
user2724280

Reputation:

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

Upvotes: 0

Tim B James
Tim B James

Reputation: 20364

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

Upvotes: 4

renanleandrof
renanleandrof

Reputation: 6997

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!"))

Upvotes: 0

djeeg
djeeg

Reputation: 6765

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

Then use PlaceHolder1.Controls.Add();

Upvotes: 0

SLaks
SLaks

Reputation: 887453

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

Upvotes: 1

Related Questions