简体   繁体   English

甚至找不到在行数据绑定网格视图上添加的复选框控件

[英]cannot find checkbox control added on rowdatabound grid view even

I have added control by 我已添加控制权

    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;
    }

and trying to find by 并试图找到

     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");
            }

        }
    }

But i cannot find it ... Actually my problem is that i need to create a dynamic list.. for that i am using gridview 但是我找不到它...实际上我的问题是我需要创建一个动态列表..为此,我正在使用gridview

You need to create dynamic controls on every postback since it is disposed at the end of the current life-cycle. 您需要在每个回发中创建动态控件,因为它已在当前生命周期的结尾处处理。 But RowDataBound is just triggered when you databind the GridView what is done typically only if(!IsPostBack) (at the first time the page loads). 但是,当您对GridView进行数据绑定时,只会触发RowDataBound通常仅if(!IsPostBack) (页面首次加载)时执行此操作。

You should create dynamic controls in RowCreated instead which is called on every postback. 您应该在RowCreated创建动态控件,而不是在每次回发时都调用它。 You can databind these controls then in RowDataBound if you want, since RowCreated is triggered first. 由于RowCreated首先触发RowCreatedRowCreated可以根据需要在RowDataBound这些控件进行数据绑定。

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
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM