0

我有一个 json,我想在服务器端转换为 HTML 网格视图并将 HTML 作为字符串返回

    StringBuilder sb = new StringBuilder();
    var serializer = new JavaScriptSerializer();
    string g = serializer.Serialize(ox)
    //here convert g to html table
    sb.Append(g);
    return sb.ToString();
4

1 回答 1

0
  private String Serialize2HTMLTable(List<conversion> ox)
        {
            var serializer = new JavaScriptSerializer();


            DataTable dt = (DataTable)JsonConvert.DeserializeObject(serializer.Serialize(ox), (typeof(DataTable)));


            StringWriter sw = new StringWriter();
            HtmlTextWriter w = new HtmlTextWriter(sw);


            //Create a table
            Table tbl = new Table();
            tbl.BorderStyle = BorderStyle.Solid;
            tbl.BorderWidth = 1;
            //Create column header row
            TableHeaderRow thr = new TableHeaderRow();
            foreach (DataColumn col in dt.Columns)
            {
                TableHeaderCell th = new TableHeaderCell();
                th.Text = col.Caption;
                thr.Controls.Add(th);
            }
            tbl.Controls.Add(thr);

            //Create table rows
            foreach (DataRow row in dt.Rows)
            {
                TableRow tr = new TableRow();
                foreach (var value in row.ItemArray)
                {
                    TableCell td = new TableCell();
                    td.BorderStyle = BorderStyle.Solid;
                    td.BorderWidth = 1;
                    td.Text = value.ToString();
                    tr.Controls.Add(td);
                }
                tbl.Controls.Add(tr);
            }

            tbl.RenderControl(w);

            return sw.ToString();

        }
于 2012-11-19T11:00:54.777 回答