【问题标题】:Using string array inside HtmlTextWriter在 HtmlTextWriter 中使用字符串数组
【发布时间】:2018-04-16 23:47:40
【问题描述】:

我正在使用 ASP.NET C# 构建一个 Web 应用程序。我有一个图像网址的字符串数组。我正在尝试在页面上显示所有这些图标。它们是从 web api 中检索的,因此它们每次都不同,大约有 300 个。

我在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
            }
        }
    }
}

它工作得很好,但是,我在 Page_Load 方法中有一个名为 iconsList 的列表,我想在 Page_Load 中设置 `iconsList 之后将 static string[] words = { "Sam", "Dot", "Perls"}; 行替换为 string[] icons = iconsList.ToArray(); 之类的东西。到目前为止,我可以调用 iconWriter.GetDivElements(),它会返回正确构建的 html,但只能使用给定的字符串数组。

【问题讨论】:

    标签: c# asp.net htmltextwriter


    【解决方案1】:

    您可以向 GetDivElements 方法添加参数,该方法接受图标列表(字符串列表),并可用于创建数组并删除静态字符串 [] 字词,因为它不需要。例如:

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

    您的页面Page_Load方法如下:

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

    【讨论】:

      猜你喜欢
      • 2014-02-08
      • 2015-12-31
      • 1970-01-01
      • 1970-01-01
      • 2022-01-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-06-19
      相关资源
      最近更新 更多