Question

I am using the HtmlTextWriter to create some HTML for me. I want to test if my page actually works but it doesnt render the div.

using System; using System.Collections.Generic; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using System.IO;

public partial class web_content_notes_Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        /* Configuration Start */
        string thumb_directory = "img/thumbs";
        string orig_directory = "img/original";
        int stage_width = 600;
        int stage_height = 480;
        Random random = new Random();

        // array of allowed file type extensions
        string[] allowed_types = { "bmp", "gif", "png", "jpg", "jpeg", "doc", "xls" };

        /* Opening the thumbnail directory and looping through all the thumbs: */
        foreach (string file in Directory.GetFiles(thumb_directory))
        {
            string title = Path.GetFileNameWithoutExtension(file);
            if (allowed_types.Equals(Path.GetExtension(file)) == true)
            {
                int left = random.Next(0, stage_width);
                int top = random.Next(0, 400);
                int rotation = random.Next(-40, -40);

                if ((top > stage_height - 130) && (left > stage_width - 230))
                {
                    top -= 120 + 130;
                    left -= 230;
                }


            }

            //display the files in the directory in a label for testing
            Label1.Text = (file);

            StringWriter stringWriter = new StringWriter();

            // Put HtmlTextWriter in using block because it needs to call Dispose.
            using (HtmlTextWriter writer = new HtmlTextWriter(stringWriter))

                // The important part:
                writer.Write("<div>testing123</div>");

        }

    }
}

I want to add the various variables into the div.

How do i do this? I remember in classic asp/vbscript you had to wrap the code in <% code %> not sure if this is the case in ASP.NET / C#

Was it helpful?

Solution

I want to test if my page actually works but it doesnt render the div.

Well no, it wouldn't. Look at what you're doing:

StringWriter stringWriter = new StringWriter();
using (HtmlTextWriter writer = new HtmlTextWriter(stringWriter))
    writer.Write("<div>testing123</div>");

So, you're writing to a StringWriter. You're then doing nothing with that string writer: the text is in memory, and you're letting it get garbage collected, basically.

If you want to write to the Page's response, you'd have to write the result to Page.Response. But you should decide whether you want control the whole of the response for the request - in which case Page probably isn't terribly appropriate - or just a single control, in which case you should probably be putting your code in a custom control.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top