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:
is HtmlTextWriter writer = new HtmlTextWriter(stringWriter)
a good way to build html tags on the server side?
Is there a better way to add elements to the html other then Response.Write("");
?
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.
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.
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.
To add an HTML server controlSet the runat="server" attribute to convert the element to a control.
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.
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
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);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With