本文介绍了向gridview动态添加复选框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我试图动态地向gridview添加一个复选框。我添加了一个静态名为selectCheckbox的复选框,但我清除了另一个函数中的所有列,如果它等于null,则需要重新添加另一个复选框。在此先感谢!
Im trying to add a checkbox to a gridview dynamically. I have added a checkbox statically called selectCheckbox, but i clear all columns in another function and need to re-add another checkbox if it equals null. Thanks in advance!
protected void mainGridView_RowCreated(Object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow &&
(e.Row.RowState == DataControlRowState.Normal ||
e.Row.RowState == DataControlRowState.Alternate))
{
CheckBox selectCheckbox = (CheckBox)e.Row.Cells[1].FindControl("selectCheckbox");
if (selectCheckbox == null)
{
selectCheckbox = new CheckBox; //this is where i need to add a checkbox to the gridview!!!
}
}
}
推荐答案
protected void mainGridView_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow &&
(e.Row.RowState == DataControlRowState.Normal ||
e.Row.RowState == DataControlRowState.Alternate))
{
if (e.Row.Cells[1].FindControl("selectCheckbox") == null)
{
CheckBox selectCheckbox = new CheckBox();
//Give id to check box whatever you like to
selectCheckbox.ID = "newSelectCheckbox";
e.Row.Cells[1].Controls.Add(selectCheckbox);
}
}
}
void GridView_RowDataBound(Object sender, GridViewRowEventArgs e)
{
if(e.Row.RowType == DataControlRowType.DataRow)
{
CheckBox cbx = new CheckBox();
// bind checkbox control with gridview :
e.Row.Cells[0].Controls.Add(cbx);
}
}
确保复选框的位置。
be sure about the position of the check box.
这篇关于向gridview动态添加复选框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!