我有一個GridView,其中的列動態添加。但是,在回發時,列不再存在。動態Gridview列在回發中找不到
我知道,動態創建的項目必須在回調後重新創建,即使創建列代碼成功執行,但GridView不包含任何列。
protected void gvUsers_RowCreated(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.Header)
{ //creating column
List<Module> listModule = getModules();
foreach (Module m in listModule)
{
TemplateField tfield = new TemplateField();
tfield.HeaderTemplate = new TickColumn(m.ModuleName);
gvUsers.Columns.Add(tfield);
}
}
if (e.Row.RowType == DataControlRowType.DataRow)
{ //Creation of checkboxes in the dynamic columns
List<Module> listModule = getModules();
int i = 3; // cell postioning
foreach (Module m in listModule)
{
CheckBox cbActive = new CheckBox();
cbActive.ID = m.ModuleID.ToString();
e.Row.Cells[i].Controls.Add(cbActive); //Error<- because the cell does not have the column
i++;
}
}
}
我是否在錯誤的事件中創建了列?
不應該這樣在數據綁定事件? – Mairaj 2014-09-03 04:17:10
它被放置在RowCreateEvent中的原因是因爲[This](http://stackoverflow.com/questions/14517812/how-to-find-a-dynamic-controlradio-button-inside-a-placeholder-in-gridview )。檢查蒂姆的答案 – 2014-09-03 06:05:55