6

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(""); ?

5 Answers 5

9

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.

Sign up to request clarification or add additional context in comments.

2 Comments

i read some time a ago that its possible to save the view state in diffrent file or in database? will this techniques help with big view state?
As far as I know it is only possible with SessionState. ViewState contains the property values of page controls. It acts as persistence medium at page level. Half the time though it gets bloated with rubbish. You have to be very careful, I've seen pages with 200K+ of ViewState alone. There are some good sites (e.g. ignatu.co.uk/ViewStateDecoder.aspx) that will show the contents of your ViewState. Worth checking out.
6

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

2 Comments

yeap i tested it and that what i was looking for thank you :)
@samy, yes it works but check your viewstate. It contains the contents of the div. You have effectively added the mark-up twice. Please check my answer below if you want to avoid your pages getting potentially large.
2

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

Comments

1

You should be able to use the ASP literal control:

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

You could have that code before the page has rendered.

Hope that helps

Paul

EDIT

Sorry, I think I was mistaken, I thought you had the html with the link to the image(s) and not the actual image itself, Justin's answer would suit you if that's the case.

Comments

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.