protected Table createTable()
    {        Table table = new Table();        TableRow row = new TableRow();
        TableCell cell1 = new TableCell();
        TableCell cell2 = new TableCell();        Literal literal1 = new Literal();
        literal1.Text = "test1";
        Literal literal2 = new Literal();
        literal2.Text = "test2";        cell1.Controls.Add(literal1);
        cell2.Controls.Add(literal2);//注意这里是2        row.Cells.Add(cell1);
        row.Cells.Add(cell2);        table.Rows.Add(row);
        return table;
    }它浏览时的HTML:
<table border="0">
 <tr>
  <td>test1</td><td>test2</td>
 </tr>
</table>
----------------------------------------------
    protected Table createTable()
    {        Table table = new Table();        TableRow row = new TableRow();
        TableCell cell1 = new TableCell();
        TableCell cell2 = new TableCell();        Literal literal1 = new Literal();
        literal1.Text = "test1";
        Literal literal2 = new Literal();
        literal2.Text = "test2";        cell1.Controls.Add(literal1);
        cell2.Controls.Add(literal1);//注意这里是1        row.Cells.Add(cell1);
        row.Cells.Add(cell2);        table.Rows.Add(row);
        return table;
    }它浏览时的HTML:
<table border="0">
 <tr>
  <td></td><td>test2</td> <!--这里第一个td的内容没有了 为什么会这样??? -->
 </tr>
</table>另外
public partial class _Default : System.Web.UI.Page 
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            this.Controls.Add(createTable());
        }
    }
}为什么浏览生成的HTML是在
</body>
</html>  是在</html>后面的????
<table border="0">
<tr>
<td>test1</td><td>test2</td>
</tr>
</table>

解决方案 »

  1.   

    楼主啊,你都遇到灵异了,才拿20分来....
    createTable()是在什么地方调用的?
      

  2.   

    this.Controls.Add(createTable());
    改成this.Form.Controls.Add(createTable());
      

  3.   

    Literal literal1 = new Literal();
            literal1.Text = "test1";
            Literal literal2 = new Literal();
            literal2.Text = "test2";        cell1.Controls.Add(literal1);
            literal1 = new Literal();//必须还要再重新生成个实例对象
            literal1.Text = "test1";
            cell2.Controls.Add(literal1);//注意这里是2