我有一个 c# aspx web 应用程序,旨在动态生成其大部分组件。有这个 GridView 是为了显示一些复选框。
奇怪的是它不显示运行时的组件,而是显示名称空间,即System.UI.Webcontrols.CheckBox
.
我需要查看复选框;如何解决?
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:GridView ID="GridView1" runat="server" OnRowDataBound="GridView1_RowDataBound" />
</div>
</form>
</body>
</html>
[aspx.cs]
namespace ChkBoxOnGridView
{
public partial class WebForm1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
PopulateGridView();
}
protected void GridView1_RowDataBound(Object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
CheckBox cb = new CheckBox();
e.Row.Cells[1].Controls.Add(cb);
}
}
private void PopulateGridView()
{
GridView1.DataSource = null;
GridView1.Columns.Clear();
DataTable dt = new DataTable();
dt.Columns.Add(new DataColumn("DTA", typeof(System.String)));
dt.Columns.Add(new DataColumn("Is Visible", typeof(System.Web.UI.WebControls.CheckBox)));
CheckBox chkbx = new CheckBox();
chkbx.Checked = true;
DataRow row1 = dt.NewRow();
row1["DTA"] = "Some Text";
row1["Is Visible"] = chkbx;
dt.Rows.Add(row1);
foreach (DataColumn col in dt.Columns)
{
BoundField bField = new BoundField();
bField.DataField = col.ColumnName;
bField.HeaderText = col.ColumnName;
GridView1.Columns.Add(bField);
}
GridView1.DataSource = dt;
GridView1.DataBind();
}
}
}