Search code examples
c#asp.nethtmltextwriter

Using string array inside HtmlTextWriter


I am building a web application using ASP.NET C#. I have a string array of image urls. I am trying to display all of these icons on the page. They are retrieved from a web api so they are different every time and there are about 300 of them.

I found this example on https://www.dotnetperls.com/htmltextwriter.

IconWriter iconWriter = new IconWriter();
class IconWriter
{
    static string[] words = { "Sam", "Dot", "Perls" };

    static string GetDivElements()
    {
        // Initialize StringWriter instance.
        StringWriter stringWriter = new StringWriter();

        // Put HtmlTextWriter in using block because it needs to call Dispose.
        using (HtmlTextWriter writer = new HtmlTextWriter(stringWriter))
        {
            // Loop over some strings.
            foreach (var word in words)
            {
                // Some strings for the attributes.
                string classValue = "ClassName";
                string urlValue = "http://www.dotnetperls.com/";
                string imageValue = "image.jpg";

                // The important part:
                writer.AddAttribute(HtmlTextWriterAttribute.Class, classValue);
                writer.RenderBeginTag(HtmlTextWriterTag.Div); // Begin #1

                writer.AddAttribute(HtmlTextWriterAttribute.Href, urlValue);
                writer.RenderBeginTag(HtmlTextWriterTag.A); // Begin #2

                writer.AddAttribute(HtmlTextWriterAttribute.Src, imageValue);
                writer.AddAttribute(HtmlTextWriterAttribute.Width, "60");
                writer.AddAttribute(HtmlTextWriterAttribute.Height, "60");
                writer.AddAttribute(HtmlTextWriterAttribute.Alt, "");

                writer.RenderBeginTag(HtmlTextWriterTag.Img); // Begin #3
                writer.RenderEndTag(); // End #3

                writer.Write(word);

                writer.RenderEndTag(); // End #2
                writer.RenderEndTag(); // End #1
            }
        }
    }
}

It works perfectly, however, I have a list inside the Page_Load method called iconsList, and I want to replace the line static string[] words = { "Sam", "Dot", "Perls"}; with something like string[] icons = iconsList.ToArray(); after the `iconsList has been set in Page_Load. I can so far call iconWriter.GetDivElements() and it will return the correctly built html, but only with the given string array.


Solution

  • You can add parameter to your method GetDivElements which accepts iconsList (List of string) and which can used to create array and delete static string[] words as its not required. For example:

    public class IconWriter
    {
        static string GetDivElements(List<string> iconsList)
        {
            string[] icons = iconsList.ToArray();
            // Initialize StringWriter instance.
            StringWriter stringWriter = new StringWriter();
            // other stuff
        }
    }
    

    Your page Page_Load method will be as below:

    public Page_Load()
    {
        List<string> iconsList = GetIconsList();
        string html = IconWriter.GetDivElements(iconsList);
    }