if you are creating those controls dynamically, you have to always recreate them upon postbackthis.Controls.Add(  Table对象); //or YourFormID.Controls.Add( Table对象);TableRow obj = new TableRow();TableCell oc = new TableCell();
oc.Controls.Add (this.DataGrid1);//把datagrid1作为1列
obj.Cells.Add(oc);oc = new TableCell();
oc.Controls.Add (this.DataGrid2);
obj.Cells.Add(oc);oc = new TableCell();
oc.Controls.Add (this.DataGrid3);
obj.Cells.Add(oc);Table对象.Rows.Add(obj);

解决方案 »

  1.   

    all of the control are drop into the form(create statically),but datagrids' datasource are made dynamically, all of these code are include in the block what is verify the page is postback, so ,must the code make any modify?
      

  2.   

    试过了,不行,我这段代码是放在button的click事件中测试的,不知道是不是因为这个不行了,请您给出完整的测试代码,thank you
      

  3.   

    try something like
    <%@ Page Language="C#" %>
    <%@ Import Namespace="System.Data" %>
    <%@ Import Namespace="System.Data.SqlClient" %><form id="form1" runat="server">
    <asp:Button id=btn1 runat=server text="create" OnClick="CreateGrid"/>
    <asp:Button id=btn2 runat=server text="submit" />
    </form>
    <script runat="server">
    bool HasDynamicControl
    {
      get 
      {
    object o = ViewState["HasDynamicControl"];
    if (o==null)
    return false;
    else
    return (bool)o;
      }
      set
      {
    ViewState["HasDynamicControl"] = value;
      }
    }protected override void LoadViewState(object savedState)
    {
      base.LoadViewState(savedState);
      if (HasDynamicControl)
    CreateControls();
    }Table CreateControls()
    {
       Table t = new Table();
       t.GridLines = GridLines.Both;   TableRow tr = new TableRow();   TableCell tc = new TableCell();
       tc.Controls.Add (new DataGrid()); 
       tr.Cells.Add(tc);   tc = new TableCell();
       tc.Controls.Add (new DataGrid()); 
       tr.Cells.Add(tc);   tc = new TableCell();
       tc.Controls.Add (new DataGrid()); 
       tr.Cells.Add(tc);   t.Rows.Add(tr);
       form1.Controls.Add(t);   return t;
    }void CreateGrid(Object sender, EventArgs e)
    {
      Table t = CreateControls();
      
      SqlDataAdapter da = new SqlDataAdapter("select * from authors; select * from sales; select * from titles;",
    "server=localhost;database=pubs;uid=sa;pwd=;");
      DataSet ds = new DataSet();
      da.Fill(ds);
     
      for (int i=0; i < t.Rows[0].Cells.Count; i++)
      {
    DataGrid dg = (DataGrid) t.Rows[0].Cells[i].Controls[0];
    dg.DataSource = ds.Tables[i].DefaultView;
    dg.DataBind();
      }

      HasDynamicControl = true;
    }
    </script>
      

  4.   

    先把DataGrid加入Table,再绑定DataGrid因为DataGrid就是table(最后在客户端生成),绑定操作得最后执行
      

  5.   

    搞定了,是我自己的一个小错误,^^!
    thank you for all help
    by the way,"DataGrid dg = (DataGrid) t.Rows[0].Cells[i].Controls[0];"这里的dg跟t里面的datagrid应该是同1个对象吧,只是2个不同的引用,所以对dg的datasource操作也就是对table中的datagrid的操作,是吗?