how to append string in a HtmlTextWriter asp.net c# webform

MD TAHMID HOSSAIN picture MD TAHMID HOSSAIN · Jan 19, 2014 · Viewed 8.2k times · Source

Let say i have a function like below, which returns option tag as a string.

 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();
        }

now i have another function like below

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

how to append string in a HtmlTextWriter?

Answer

Kevin picture Kevin · Jan 19, 2014

I'm not too familiar using the HtmlTextWriter but according to msdn you should be able to do

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