CType(C, CheckBox).Checked = True
感觉不对,试试这样
((CheckBox)C).Checked = True

解决方案 »

  1.   

    net_lover(孟子E章) 請問該如何設計,給個例子好嗎?
     elliot(!逸轩) 
    這樣也不行!
      

  2.   

    When dynamically adding controls to an ASP.NET page in runtime the object references are lost at postback because they have no handle in the codebehind. The values entered by the user is not availible when accessing these objects after postback, and they seem empty. This article describes how to use ViewState to recreate and reinsert the postback values into the objects to access their values. http://www.codeproject.com/aspnet/RetainingState.asp
      

  3.   

    這樣也不行,我要改變checkbox的checked值
      

  4.   

    动态生成的在postback后在服务器是找不到的!
    也没什么特别好的办法!
      

  5.   

    有办法,可以让它保持的。用SaveViewState方法
      

  6.   

    net_lover(孟子E章)麻煩你告訴我怎么寫?
      

  7.   

    我在msdn下载了一篇文章,我着着看,就是说的这个问题
      

  8.   

    从前一个由 SaveViewState 方法保存的用户控件请求还原视图状态信息。[Visual Basic]
    Overrides Protected Sub LoadViewState( _
       ByVal savedState As Object _
    )
    [C#]
    protected override void LoadViewState(
       object savedState
    );
    [C++]
    protected: void LoadViewState(
       Object* savedState
    );
    [JScript]
    protected override function LoadViewState(
       savedState : Object
    );
    参数
    savedState 
    表示要还原的用户控件状态的 Object。 
    备注
    一般情况下,无需调用此方法。不过,如果在视图状态中存储自定义信息,可以重写此方法以实现此目的。例如,可以将视图状态值加载到字段中,这样将来就不必从 Control.ViewState 属性检索它。还可以在即将调用 base.SaveViewState 前将该值插入 ViewState 属性,这对于在与服务器的往返行程间保持字段是一个有效的方法。示例
    [Visual Basic, C#] 下面的示例说明使用 LoadViewState 和 SaveViewState 方法管理其视图状态的用户控件。[Visual Basic] Public ReadOnly Property UserText() As String
       Get
          Return CStr(ViewState("usertext"))
       End Get
    End PropertyPublic ReadOnly Property PasswordText() As String
       Get
          Return CStr(ViewState("passwordtext"))
       End Get
    End Property
    Protected Overrides Sub LoadViewState(savedState As Object)
       If Not (savedState Is Nothing) Then
          MyBase.LoadViewState(savedState)
       End If
    End Sub 
     
    Protected Overrides Function SaveViewState() As Object
       ViewState("usertext") = user.Text
       ViewState("passwordtext") = password.Text
       Return MyBase.SaveViewState()
    End Function 
    End Class
    [C#] 
    public string UserText
    {
       get
       {
          return (string)ViewState["usertext"];
       }
    }
    public string PasswordText
    {
       get
       {
          return (string)ViewState["passwordtext"];
       }
    }protected override void LoadViewState(object savedState) 
    {
       if (savedState != null)
          base.LoadViewState(savedState);
    }protected override object SaveViewState()
    {
       ViewState["usertext"] = user.Text;
       ViewState["passwordtext"] = password.Text;
       return base.SaveViewState();
    }
      

  9.   

    下面具体的例子:
    Adding Columns DynamicallyYou can hide and show columns if you know in advance what columns you need. Sometimes, however, you do not know that until run time. In that case, you can create columns dynamically and add them to the grid.To do so, you create an instance of one of the column classes supported by the grid — BoundColumn, EditCommandColumn, ButtonColumn, or HyperlinkColumn. (You can add template columns to the grid, but it is slightly more complex. For details, see Creating Web Server Control Templates Programmatically.) Set the column's properties, and then add it to the grid's Columns collection.The following example shows how to add two bound columns to a grid.' Visual Basic
    Private Sub Button1_Click(ByVal sender As System.Object, _
          ByVal e As System.EventArgs) Handles Button1.Click
       'Set data-binding properties of the grid
       DataGrid1.AutoGenerateColumns = False
       DataGrid1.DataSource = Me.dsBooks1
       DataGrid1.DataMember = "Books"
       DataGrid1.DataKeyField = "bookid"   ' Add two columns
       Dim dgc_id As New BoundColumn()
       dgc_id.DataField = "bookid"
       dgc_id.HeaderText = "ID"
       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.SqlDataAdapter1.Fill(Me.dsBooks1)
       DataGrid1.DataBind()
    End Sub// C#
    private void Button1_Click(object sender, System.EventArgs e)
    {
       DataGrid1.AutoGenerateColumns = false;
       DataGrid1.DataSource = this.dsBooks1;
       DataGrid1.DataMember = "Books";
       DataGrid1.DataKeyField = "bookid";   // Add two columns
       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();
    }
    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;
    }