Solved

How can I show images using stringbuilder and htmltextwriter?

Posted on 2009-05-13
15
2,851 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
  • 8
  • 7
15 Comments
 
LVL 11

Expert Comment

by:azarc3
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:azarc3
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
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 

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:azarc3
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:azarc3
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:azarc3
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:
azarc3 earned 500 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:azarc3
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:azarc3
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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …

777 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