Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

How can I show images using stringbuilder and htmltextwriter?

Posted on 2009-05-13
15
Medium Priority
?
3,326 Views
Last Modified: 2012-05-06
The following code works to write some HTML for an outpur page I created.  The only factor that seems to be missing in the output are some of my images.  is there a wau to bring in the images as well?
StringBuilder   sb = new StringBuilder ();
                StringWriter    swr = new StringWriter (sb);
                HtmlTextWriter  hwr = new HtmlTextWriter (swr);
                base.Render (hwr);
                string html = sb.ToString ();
                writer.Write (html);

Open in new window

0
Comment
Question by:devnewbee
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 8
  • 7
15 Comments
 
LVL 11

Expert Comment

by:Kelvin McDaniel
ID: 24376958
I assume you mean physical images that exist in the website you're outputting the page for, NOT a binary stream that would be rendered as an image.

You're going to need to make sure the images each have a valid path that is correct for the page you're rendering. For example...

This should work, assuming your images folder is in the root of the website:
sb.Append("<img src=\"/images/image1.jpg\" />");

This will NOT work, because HTML doesn't understand the significance of the tilde:
sb.Append("<img src=\"~/images/image1.jpg\" />");

This should work, assuming your images folder is in the root of the website:
sb.Append("<img src=\"" + ResolveUrl("~/images/image1.jpg") + "\" />");
0
 

Author Comment

by:devnewbee
ID: 24377001
So, if I use...

sb.Append("<img src=\"/files/image1.jpg\" />");

this should work?

Where is the stringbuilder syntax exactly do I place it and is there any other syntax required?
0
 
LVL 11

Expert Comment

by:Kelvin McDaniel
ID: 24377049
First, I 've got a question for you:

In your sample I don't see where you're actually populating the StringBuilder with your html code; did you simply not include that part in your question snippet? Otherwise, where is it coming from?
0
Prepare for your VMware VCP6-DCV exam.

Josh Coen and Jason Langer have prepared the latest edition of VCP study guide. Both authors have been working in the IT field for more than a decade, and both hold VMware certifications. This 163-page guide covers all 10 of the exam blueprint sections.

 

Author Comment

by:devnewbee
ID: 24377097
I am overiding the render.  Please see below...

    protected override void Render(HtmlTextWriter writer)


0
 
LVL 11

Expert Comment

by:Kelvin McDaniel
ID: 24377167
Which is applicable here?

A. The passed in HtmlTextWriter writer already has the HTML you're going to render
- OR -
B. You're going to create the HTML on the fly in your Render() method?
0
 

Author Comment

by:devnewbee
ID: 24377530
Its actually based on if the page is a postback and the selected value of a control.
0
 

Author Comment

by:devnewbee
ID: 24377619
B. You're going to create the HTML on the fly in your Render() method?
0
 
LVL 11

Expert Comment

by:Kelvin McDaniel
ID: 24377851
Ok. As long as you're going to have the opportunity to inject the <img> code then this is an easy(ier) thing to do. Back to your first response, yes, the following will work.

sb.Append("<img src=\"/files/image1.jpg\" />");

The StringBuilder syntax that you'll want to use i this case fits in as follows (consider this option #1):

StringBuilder sb = new StringBuilder ();
sb.Append({whatever_other_html_comes_before_the_image});
sb.Append("<img src=\"/files/image1.jpg\" />");
sb.Append({whatever_other_html_comes_after_the_image});
 
StringWriter swr = new StringWriter (sb);
HtmlTextWriter hwr = new HtmlTextWriter (swr);
base.Render (hwr);
string html = sb.ToString ();
writer.Write (html);

Open in new window

0
 
LVL 11

Expert Comment

by:Kelvin McDaniel
ID: 24377971
Consider this option #2:
(Note: this option assumes only one image will be shown and that it has a place marker of {0} where it is supposed to appear. This can be easily extended to a very large number of images using the basis)
StringBuilder sb = new StringBuilder ();
sb.Append(string.format("{whatever_other_html_is_to_be_included}", "/files/image1.jpg");
 
StringWriter swr = new StringWriter (sb);
HtmlTextWriter hwr = new HtmlTextWriter (swr);
base.Render (hwr);
string html = sb.ToString ();
writer.Write (html);

Open in new window

0
 
LVL 11

Accepted Solution

by:
Kelvin McDaniel earned 2000 total points
ID: 24378059
Here's an example of where you would expect many images.
        ArrayList aryImages = new ArrayList();
        aryImages.Add("/files/image1.jpg"); // will be substituted for the "{0}" placeholder
        //... however many you need for your HTML
        aryImages.Add("/files/imageN.jpg"); //matches placeholder "{N}" where N is the count of images.
 
        StringBuilder sb = new StringBuilder();
        sb.Append(string.Format("{whatever_other_html_is_to_be_included}", aryImages.ToArray()));
 
        StringWriter swr = new StringWriter(sb);
        HtmlTextWriter hwr = new HtmlTextWriter(swr);
        base.Render(hwr);
        string html = sb.ToString();
        writer.Write(html);

Open in new window

0
 

Author Closing Comment

by:devnewbee
ID: 31581071
Awesome.  Thanks for the help!
0
 

Author Comment

by:devnewbee
ID: 24378544
Can you elaborate a little more on the placeholder syntax?
0
 
LVL 11

Expert Comment

by:Kelvin McDaniel
ID: 24378637
Yep. It goes in order... so you start at {0} where you want the first subsitution to be. The next will be {1}, and the last will be {N} where N is the count of the items minus 1 (because we started at 0).

You MUST has the same number of items to substitutes as you have placeholders, and vice versa.
0
 

Author Comment

by:devnewbee
ID: 24378691
So, if I have this right, for three images it would be...just want to clarify.

 ArrayList aryImages = new ArrayList();
        aryImages.Add("/files/bg0.jpg"); // will be substituted for the "{0}" placeholder
        //... however many you need for your HTML
        aryImages.Add("/files/image1.jpg"); //matches placeholder "{N}" where N is the count of images.
         aryImages.Add("/files/footer2.jpg"); //matches placeholder "{N}" where N is the count of images.
 

0
 
LVL 11

Expert Comment

by:Kelvin McDaniel
ID: 24402940
That is exactly right. In your composite string that the String Builder will eventually contain needs to include {0}, {1}, and {2}... Otherwise you'll get a runtime error.
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…

722 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question