我在网格视图列中添加了一个动态创建复选框,但它在按钮单击时为我提供了空值。
回发后复选框消失。
这是我的代码
protected void grdreport_RowDataBound(object sender, GridViewRowEventArgs e)
{
int temp = e.Row.Cells.Count;
temp--;
if (e.Row.RowType == DataControlRowType.DataRow)
{
if (temp >= 3)
{
strheadertext1 = grdreport.HeaderRow.Cells[3].Text;
CheckBox cb1 = new CheckBox();
cb1.Text = e.Row.Cells[3].Text;
e.Row.Cells[3].Controls.Add(cb1);
}
}
}
并在我的按钮上单击我检查复选框的值是否被选中
foreach (GridViewRow item in grdreport.Rows)
{
if (item.RowType == DataControlRowType.DataRow)
{
CheckBox checkbox1 = (CheckBox)item.FindControl("cb1");
// cb1.Checked = true;
if (checkbox1.Checked)
{
}
}
}
使用动态控件时,您需要在每个 PostBack 上重新绑定 GridView 数据。所以通常你会使用 IsPostBack 检查并在那里绑定数据。但现在不要这样做。
protected void Page_Load(object sender, EventArgs e)
{
//normally you would bind here
if (IsPostBack == false)
{
GridView1.DataSource = source;
GridView1.DataBind();
}
//but when using dynamic control inside a gridview, bind here
GridView1.DataSource = source;
GridView1.DataBind();
}
更新
你必须给一个动态控件一个 ID。您正在寻找cb1
,但您从未将该 ID 分配给复选框。
CheckBox cb1 = new CheckBox();
cb1.ID = "cb1";
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句