如何在HtmlTextWriter asp.net c#webform中追加字符串

时间:2014-01-19 06:52:20

标签: c# asp.net webforms htmltextwriter

假设我有一个类似下面的函数,它将选项标签作为字符串返回。

 public static string writeDropDownOptionHTML(string tablename, string id_col, string value_col)
        {
            StringWriter stringwriter = new StringWriter();
            HtmlTextWriter writer = new HtmlTextWriter(stringwriter);
            DataTable dt1 = BAL.setDropDown(tablename, id_col, value_col);
            if (dt1.Rows.Count > 0)
            {
                foreach (DataRow row in dt1.Rows)
                {
                    writer.AddAttribute(HtmlTextWriterAttribute.Value, row[0].ToString());
                    writer.RenderBeginTag(HtmlTextWriterTag.Option);

                    writer.Write(row[1].ToString());
                    writer.RenderEndTag();
                }
            }

            return stringwriter.ToString();
        }

现在我有另一个功能,如下面的

public static string writeWalkReverseTableData(DataTable dt1)
        {
            StringWriter stringwriter = new StringWriter();
            HtmlTextWriter writer = new HtmlTextWriter(stringwriter);

            if (dt1.Rows.Count > 0)
            {
                foreach (DataRow row in dt1.Rows)
                {

                    writer.RenderBeginTag(HtmlTextWriterTag.Tr);
                    writer.RenderBeginTag(HtmlTextWriterTag.Td);
                    writer.RenderBeginTag(HtmlTextWriterTag.Select);
                    // now i want to call  writeDropDownOptionHTML which will return option html code
                    // writer += writeDropDownOptionHTML("xyz","abc","def"); 
                    writer.RenderEndTag();
                    writer.RenderEndTag();
                    writer.RenderEndTag();
                }
            }
            return stringwriter.ToString();
        }

enter image description here

如何在HtmlTextWriter中附加字符串?

2 个答案:

答案 0 :(得分:1)

我不太熟悉使用HtmlTextWriter,但根据msdn你应该可以做到

writer.Write(writeDropDownOptionHTML("xyz","abc","def"));

答案 1 :(得分:1)

我将 writer 从writeWalkReverseTableData传递给writeDropDownOptionHTML作为参数,如下所示:

    public static void writeDropDownOptionHTML(HtmlTextWriter writer, string tablename, string id_col, string value_col)
            {
                DataTable dt1 = BAL.setDropDown(tablename, id_col, value_col);
                if (dt1.Rows.Count > 0)
                {
                    foreach (DataRow row in dt1.Rows)
                    {
                        writer.AddAttribute(HtmlTextWriterAttribute.Value, row[0].ToString());
                        writer.RenderBeginTag(HtmlTextWriterTag.Option);

                        writer.Write(row[1].ToString());
                        writer.RenderEndTag();
                    }
                }
            }

public static string writeWalkReverseTableData(DataTable dt1)
        {
            StringWriter stringwriter = new StringWriter();
            HtmlTextWriter writer = new HtmlTextWriter(stringwriter);

            if (dt1.Rows.Count > 0)
            {
                foreach (DataRow row in dt1.Rows)
                {

                    writer.RenderBeginTag(HtmlTextWriterTag.Tr);
                    writer.RenderBeginTag(HtmlTextWriterTag.Td);
                    writer.RenderBeginTag(HtmlTextWriterTag.Select);
                    writeDropDownOptionHTML(writer, "xyz","abc","def"); 
                    writer.RenderEndTag();
                    writer.RenderEndTag();
                    writer.RenderEndTag();
                }
            }
            return stringwriter.ToString();
        }