Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

adding html elements with in asp.net

Tags:

c#

asp.net

I need some help figuring out how to do something.

I got this gallery (galleriffic) and some images that are store in Flicker.com so I used the flicker api to get the images but still add them manually to test the gallery.

Now I'm looking for a good way to insert the images into the html after I get them with the flicker api.

I found this htmltextwriter and used the function

Response.Write(GetDivElements());

but this is adding the div's on the top of the html and not inside the body tag.

my qustions is:

  1. is HtmlTextWriter writer = new HtmlTextWriter(stringWriter) a good way to build html tags on the server side?

  2. Is there a better way to add elements to the html other then Response.Write(""); ?

like image 608
samy Avatar asked Feb 28 '12 14:02

samy


People also ask

How can add HTML code behind in asp net?

Create same html as you want to display on screen and store it in variable TEMP. You can take html creation of control in separate function based on requirement. Place that created html as innerHTML to your panel/div.

How dynamically add HTML controls in asp net?

You can set the InnerHtml attribute of just about any control. Whenyou need to have dynamic HTML, better to have a div in aspx page with an id and runat="server" then edit the HTML as You need in code-behind.

Can we use HTML in asp net?

By default, HTML elements on an ASP.NET Web page are not available to the server. These components are treated as simple text and pass through to the browser. We can convert an HTML element to server control by adding a runat="server" and an id attribute to the component. Now, we can easily access it at code behind.

How do you convert an HTML element on the page to a HTML server control?

To add an HTML server controlSet the runat="server" attribute to convert the element to a control.


3 Answers

Here is what I do when I need to add mark-up.

in my page

<asp:PlaceHolder ID="MyPlaceholder" runat="server"></asp:PlaceHolder>

in my code behind

MyPlaceholder.Controls.Add(new Literal() { Text="<div>some markup</div>"});

I do it this way because:

1) you can put the PlaceHolder where you need it in the structure of your page

2) by adding a Literal at runtime to the Controls collection prevents ViewState getting bloated with it's contents.

like image 67
Dave Becker Avatar answered Oct 26 '22 23:10

Dave Becker


If you are using the older style of asp.net, and not asp.net MVC, then you can just create a div with an id and runat="server". Then you can just write directly to the html.

aspx page

<div id = "DivINeedToAddStuffTo" runat="server" />

aspx.cs

DivINeedToAddStuffTo.InnerHtml = GetDivElements();   

Also, I do not see anything wrong with using HtmlTextWriter to create your Html markup

like image 39
Justin Pihony Avatar answered Oct 27 '22 01:10

Justin Pihony


You might try looking into Placeholders. That way you can create an instance of an image control and then add it your your placeholder.

Image myImg = new Image(); 
myImg.ImageUrl = "MyPicture.jpg"; 
myPlaceholder.Controls.Add(myImg);
like image 23
Jason Lattimer Avatar answered Oct 27 '22 00:10

Jason Lattimer