Solved

How can I show images using stringbuilder and htmltextwriter?

Posted on 2009-05-13
15
2,783 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
 

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
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.
As a trusted technology advisor to your customers you are likely getting the daily question of, ‘should I put this in the cloud?’ As customer demands for cloud services increases, companies will see a shift from traditional buying patterns to new…

864 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now