【发布时间】:2012-03-10 04:52:17
【问题描述】:
我正在尝试将字段更改为 GridView 中的复选框。
我目前根据查询动态创建网格列,并且我想将一些列更改为复选框,以便用户可以选中/取消选中它。我知道我不能通过 .aspx 页面使用但我试图远离静态创建字段。
任何帮助都会很棒。
【问题讨论】:
我正在尝试将字段更改为 GridView 中的复选框。
我目前根据查询动态创建网格列,并且我想将一些列更改为复选框,以便用户可以选中/取消选中它。我知道我不能通过 .aspx 页面使用但我试图远离静态创建字段。
任何帮助都会很棒。
【问题讨论】:
利用 GridView 的 RowDataBound 事件。因此,您可以将任何控件添加到您的 GridView。
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
CheckBox chk1 = new CheckBox();
chk1.ID = "chkbox1";
e.Row.Cells[0].Controls.Add(chk1);
}
}
编辑评论:
将值从数据库传递到 gridview 后(超出此问题的范围),您可以使用 e.Row.Cells[i].Text 访问这些值,其中“i”是行。
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
TextBox txt1 = new TextBox();
txt1.Text = e.Row.Cells[0].Text;
e.Row.Cells[0].Controls.Add(txt1);
}
}
【讨论】: