关于使用datalist的一个问题

zjnfast 2003-06-22 05:00:04
我在datalist里面绑定了一些控件,如radiobutton,checkbox和textbox,
但是在使用的时候却无法得到这些控件的一些相关值,
radio和check的checked始终为false,不管我是否选择,
textbox的text值也是空的
但是可以得到radio和checkbox的text值。
那位能解决这个问题,多多给分。
...全文
66 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
zjnfast 2003-06-22
  • 打赏
  • 举报
回复
请问net_lover(孟子E章):
我在另外一个页面中使用了一个datagrid,里面也是嵌入了一个checkbox,但是这时候checkbox的状态(checked)就可以保存啊。

那datagrid和datelist为什么会不一样呢?
孟子E章 2003-06-22
  • 打赏
  • 举报
回复
例子:
Adding Columns Dynamically
You 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 Property

Protected Overrides Sub LoadViewState(ByVal savedState As Object)
MyBase.LoadViewState(savedState)
If Me.DynamicColumnAdded Then
Me.AddColums()
End If
End Sub

Private 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 Sub

Protected 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;
}
孟子E章 2003-06-22
  • 打赏
  • 举报
回复
动态增加的控件的ViewState不会被保存的。必须自己处理

110,568

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧