gpt4 book ai didi

c# - 甚至找不到在 rowdatabound GridView 上添加的复选框控件

转载 作者:太空宇宙 更新时间:2023-11-03 13:47:38 25 4
gpt4 key购买 nike

我已经添加了控件

    if (e.Row.RowType == DataControlRowType.DataRow)
{
CheckBox chk = new CheckBox();
chk.EnableViewState = true;
chk.Enabled = true;
chk.ID = "chkb";


DataRowView dr = (DataRowView)e.Row.DataItem;
chk.Checked = (dr[0].ToString() == "true");


e.Row.Cells[1].Controls.Add(chk);
e.Row.TableSection = TableRowSection.TableBody;
}

并试图找到

     if (GridView2.Rows.Count>0)
{
foreach (GridViewRow row in GridView2.Rows)
{
CheckBox cb =(CheckBox) GridView2.Rows[2].Cells[1].FindControl("chkb");
if (cb != null && cb.Checked)
{
Response.Write("yest");
}

}
}

但是我找不到...实际上我的问题是我需要创建一个动态列表.. 为此我正在使用 gridview

最佳答案

您需要在每个回发上创建动态控件,因为它是在当前生命周期结束时处理的。但是 RowDataBound 只是在您对 GridView 进行数据绑定(bind)时触发,通常只在 if(!IsPostBack) 中完成(在第一次加载页面时) .

您应该在 RowCreated 中创建动态控件,而不是在每次回发时调用它。如果需要,您可以在 RowDataBound 中对这些控件进行数据绑定(bind),因为首先触发 RowCreated

protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
CheckBox chk = new CheckBox();
chk.EnableViewState = true;
chk.Enabled = true;
chk.ID = "chkb";
e.Row.Cells[1].Controls.Add(chk);
}
}

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
var chk = (CheckBox)e.Row.FindControl("chkb");
// databind it here according to the DataSource in e.Row.DataItem
}
}

关于c# - 甚至找不到在 rowdatabound GridView 上添加的复选框控件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14752629/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com