我有一個代碼:e.Row.Cell增加控制不起作用
protected void gvContacts_RowDatabound(object sender, GridViewRowEventArgs e)
{
Label label = new Label();
label.Text = "test";
if (e.Row.RowType == DataControlRowType.DataRow && e.Row.RowIndex == 0)
{
for (int i = 0; i < e.Row.Cells.Count; i++)
{
e.Row.Cells[i].Controls.Add(label); //doesnt work
e.Row.Cells[i].Text = "this works";
}
}
}
其中標籤不顯示我的細胞。怎麼了?
你在哪裏試圖調用這段代碼?具體來說,它是在RowDataBind中調用的嗎? –
你在哪寫這段代碼?你無法在任何地方添加控件。 [ASP.NET頁面生命週期](http://msdn.microsoft.com/en-us/library/ms178472.aspx)可能非常複雜。 –
更新我的代碼 –