Adding html elements to asp.net

I need help on how to do this.

I got this gallery (galleriffic) and some images stored on Flicker.com, so I used the api flicker to get the images, but still add them manually to check the gallery.

Now I am looking for a good way to insert images in html after I get them using the api flicker.

I found this htmltextwriter and used the function

Response.Write(GetDivElements());

but this adds the div to the top of the html and not inside the body tag.

my questions:

  • is HtmlTextWriter writer = new HtmlTextWriter(stringWriter) good way to create server side html tags?

  • Is there a better way to add elements to the html to another, and then Response.Write(""); ?

+4
source share
5 answers

If you are using the older asp.net style rather than asp.net MVC, then you can just create a div with id and runat = "server". Then you can simply write directly in html.

aspx page

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

aspx.cs

 DivINeedToAddStuffTo.InnerHtml = GetDivElements(); 

Also, I see nothing wrong with using HtmlTextWriter to create your HTML markup

+4
source

Here is what I do when I need to add markup.

on my page

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

in my code

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

I do it like this because:

1) you can place PlaceHolder where necessary in the structure of your page

2) by adding Literal at run time to the Controls collection, it prevents the inflation of the ViewState with its contents.

+7
source

You can try exploring Placeholders . This way you can create an instance of the image control and then add it with your placeholder.

 Image myImg = new Image(); myImg.ImageUrl = "MyPicture.jpg"; myPlaceholder.Controls.Add(myImg); 
+2
source

You should be able to use the ASP letter listing:

  foreach (var item in items) { Literal literal = new Literal(); literal.text = item.html; //Assuming the item contains the html. MyPlaceholder.Controls.Add(literal); } 

You may have this code before the page displays.

Hope that helps

Floor

EDIT

Sorry, I think I was wrong, I thought that you have html with a link to the image, and not with the image itself, Justin's answer will suit you if this happens.

+1
source
  var ctrl = new WebControl(HtmlTextWriterTag.Div) { CssClass = "SomeClass" }; ctrl.Attributes["style"] = "float:left;display:inline-block;margin:3px;"; ctrl.Controls.Add(new Image { ImageUrl = Page.ResolveUrl("image path here") }); this.Controls.Add(ctrl); 
+1
source

Source: https://habr.com/ru/post/1398826/


All Articles