动态添加单元格与控件

来源:互联网 发布:泛神论 知乎 编辑:程序博客网 时间:2024/05/16 12:58
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default4.aspx.cs" Inherits="Default4" %><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head runat="server">    <title>无标题页</title></head><body>    <form id="form1" runat="server">    <table>    <div id="divFlow" runat ="server" >            </div>    </table>    </form></body></html>

using System;using System.Collections;using System.Configuration;using System.Data;using System.Linq;using System.Web;using System.Web.Security;using System.Web.UI;using System.Web.UI.HtmlControls;using System.Web.UI.WebControls;using System.Web.UI.WebControls.WebParts;using System.Xml.Linq;public partial class Default4 : System.Web.UI.Page{    protected void Page_Load(object sender, EventArgs e)    {        if (!this.IsPostBack) {            BuildRows();        }    }    private void BuildRows()    {        TableRow tr = new TableRow();        TableHeaderCell th = new TableHeaderCell();        TableCell td = new TableCell();        TextBox tb = new TextBox();        tb.Text = "测试";        td.Controls.Add(tb);        th.Text = "<img alt='image' src='' />";        tr.Controls.Add(th);        tr.Controls.Add(td);        this.divFlow.Controls.Add(tr);    }}
using System;using System.Collections;using System.Configuration;using System.Data;using System.Linq;using System.Web;using System.Web.Security;using System.Web.UI;using System.Web.UI.HtmlControls;using System.Web.UI.WebControls;using System.Web.UI.WebControls.WebParts;using System.Xml.Linq;using System.Text.RegularExpressions;public partial class Default4 : System.Web.UI.Page{    protected void Page_Load(object sender, EventArgs e)    {        if (!this.IsPostBack)        {            BuildRows();        }    }    private void BuildRows()    {        String html =@"<tr><th>只有标题</th></tr><tr><td>只有td</td></tr><tr><th>标题:</th><td>第1格</td></tr><tr><th>标题:</th><td></td></tr>";        Table table = TableHtmlToTalbeControl(html);        this.divFlow.Controls.Add(table);    }    private Table TableHtmlToTalbeControl(string tableHtml)    {        string regTr = "<tr>.*?</tr>";        string regTh = "<th>.*?</th>";        string regTd = "<td>.*?</td>";        MatchCollection mc = Regex.Matches(tableHtml, regTr            , RegexOptions.IgnoreCase | RegexOptions.Multiline | RegexOptions.Singleline);        Table table = new Table();        foreach (Match mh in mc)        {            TableRow tr = new TableRow();            MatchCollection mcTh = Regex.Matches(mh.Value, regTh                , RegexOptions.IgnoreCase | RegexOptions.Multiline | RegexOptions.Singleline);            MatchCollection mcTd = Regex.Matches(mh.Value, regTd                , RegexOptions.IgnoreCase | RegexOptions.Multiline | RegexOptions.Singleline);            if (mcTh.Count == 0 && mcTd.Count == 0)                continue;            if (mcTh.Count == 0)            {                for (int i = 0; i < mcTd.Count; i++)                {                    Regex regex = new Regex("<.+?>", RegexOptions.IgnoreCase);                    Match mhTd = mcTd[i];                    TableCell td = new TableCell();                    td.Text = regex.Replace(mhTd.Value, "");                    tr.Controls.Add(td);                }            }            else if (mcTd.Count == 0)            {                for (int i = 0; i < mcTh.Count; i++)                {                    Regex regex = new Regex("<.+?>", RegexOptions.IgnoreCase);                    Match mhTh = mcTh[i];                    TableHeaderCell th = new TableHeaderCell();                    th.Text = regex.Replace(mhTh.Value, "");                    tr.Controls.Add(th);                }            }            else            {                for (int i = 0; i < mcTh.Count; i++)                {                    Regex regex = new Regex("<.+?>", RegexOptions.IgnoreCase);                    Match mhTh = mcTh[i];                    TableHeaderCell th = new TableHeaderCell();                    th.Text = regex.Replace(mhTh.Value, "");                    tr.Controls.Add(th);                    Match mhTd = mcTd[i];                    TableCell td = new TableCell();                    td.Text = regex.Replace(mhTd.Value, "");                    tr.Controls.Add(td);                }            }            table.Controls.Add(tr);        }        return table;    }}