不知用直接写好的页面动态调用能否解决。
下面是一个frame,根据需要进行刷新。

解决方案 »

  1.   

    动态加载的控件,ViewState是True还是False?问题是否出在这儿?
    ===========================================================THINGS ARE NOT ALWAYS WHAT THEY SEEMThinking in ......
      

  2.   

    说的简单些,我在一个Panel里必须先删除以前的UserControl,再添加新的,(以上代码写在Page_Load中),但我发现新加的UserControl的ViewSatate并没有保存,因为我随意PostBack,他就会变为UserControl最初的状态,直到下一次PostBack他才会保存下来,怎么办?有没有办法在第一次PostBack之前就强制性的记录它的ViewState.
      

  3.   

    Any time that you add controls to a page dynamically, you have the problem of persistence. Dynamically-added controls (or in this case, columns) are NOT automatically added to the page's view state, so you are obliged to add logic to the page to make sure the columns are available with each round trip.An excellent way to do this is to override the page's LoadViewState method, which gives you an early opportunity to reestablish columns in the DataGrid control. Because the LoadViewState method is called before the Page_Load event is raised, re-adding columns in the LoadViewState method assures that they are available for normal manipulation by the time any event code runs.The following example shows how you would expand the previous example to restore the columns each time the page runs again. As before, the Button1_Click handler adds two columns to the grid. (In this example, the event handler calls a separate routine called AddColumns to do so.) In addition, the page contains a simple Boolean property called DynamicColumnsAdded indicating whether the grid has had columns added; the property persists its value in view state. The LoadViewState method first calls the base class's LoadViewState method, which extracts view state information and configures controls with it. If columns were previously added to the grid (as per the DynamicColumnsAdded property), the method then re-adds them.' Visual Basic
    Private Property DynamicColumnAdded() As Boolean
       Get
          If ViewState("ColumnAdded") Is Nothing Then
             Return False
          Else
             Return True
          End If
       End Get
       Set(ByVal Value As Boolean)
          ViewState("ColumnAdded") = Value
       End Set
    End PropertyProtected Overrides Sub LoadViewState(ByVal savedState As Object)
       MyBase.LoadViewState(savedState)
       If Me.DynamicColumnAdded Then
          Me.AddColums()
       End If
    End SubPrivate Sub Button1_Click(ByVal sender As System.Object, _
          ByVal e As System.EventArgs) Handles Button1.Click
       ' Check property to be sure columns are not added more than once
       If Me.DynamicColumnAdded Then
          Return
       Else
          Me.AddColums()
       End If
    End SubProtected Sub AddColums()
       ' Add two columns
       Dim dgc_id As New BoundColumn()
       dgc_id.DataField = "instock"
       dgc_id.HeaderText = "In Stock?"
       dgc_id.ItemStyle.Width = New Unit(80)
       DataGrid1.Columns.Add(dgc_id)   Dim dgc_title As New BoundColumn()
       dgc_title.DataField = "title"
       dgc_title.HeaderText = "Title"
       DataGrid1.Columns.Add(dgc_title)
       Me.DataGrid1.DataBind()
       Me.DynamicColumnAdded = True
    End Sub// C#
    private bool DynamicColumnAdded{
       get
       {
          object b = ViewState["DynamicColumnAdded"];
          return (b == null) ? false : true;
       }
       set
       {
          ViewState["DynamicColumnAdded"] = value;
       }
    }protected override void LoadViewState(object savedState)
    {
       base.LoadViewState(savedState);
       if (DynamicColumnAdded) 
       {
          this.AddColumns();
       }
    }private void Button1_Click(object sender, System.EventArgs e)
    {
       if(this.DynamicColumnAdded != true)
       {
          this.AddColumns();
       }
    }private void AddColumns()
    {
       BoundColumn dgc_id = new BoundColumn();
       dgc_id.DataField = "bookid";
       dgc_id.HeaderText = "ID";
       dgc_id.ItemStyle.Width = new Unit(80);
       DataGrid1.Columns.Add(dgc_id);   BoundColumn dgc_title= new BoundColumn();
       dgc_title.DataField = "title";
       dgc_title.HeaderText = "Title";
       DataGrid1.Columns.Add(dgc_title);   this.sqlDataAdapter1.Fill(this.dsBooks1);
       DataGrid1.DataBind();
       this.DynamicColumnAdded = true;
    }