我有問題,我無法獲得我在DataGrid中添加的控件。我將它添加在OnRowDataBound事件,如:asp:gridview不包含添加在OnRowDataBound中的控件
protected void RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowState == DataControlRowState.Edit || e.Row.RowState == (DataControlRowState.Alternate | DataControlRowState.Edit))
{
//int cindex = 0;
//for (cindex = 0; cindex < e.Row.Controls.Count; cindex++)
foreach (Control ctl in e.Row.Controls)
{
DataControlFieldCell dcctl = (DataControlFieldCell)ctl;
TableCell tcell = (TableCell)dcctl;
Label lblComment = new Label();
TextBox txtComment = new TextBox();
lblComment.Text = "<br>Comment: ";
dcctl.Controls.Add(lblComment);
dcctl.Controls.Add(txtComment);
//tcell.Controls.Add(lblComment);
//tcell.Controls.Add(txtComment);
//e.Row.Cells[cindex].Controls.Add(lblComment);
//e.Row.Cells[cindex].Controls.Add(txtComment);
這裏發生的事情:有一個已經存在默認情況下的TableCell一個文本框,我想再添一個文本框和標籤。在邊界後我可以看到2個文本框,我可以將數據輸入到兩個文本框中,但是當我單擊更新按鈕時,然後引發OnRowUpdating
事件,因爲我無法獲取我的TextBox!
protected void RowUpdating(object sender, GridViewUpdateEventArgs e)
{
grdView.EditIndex = -1;
int counter = 0;
for (counter = 0; counter < grdView.Rows[e.RowIndex].Cells.Count; counter++)
{
foreach (Control ctl in grdView.Rows[e.RowIndex].Cells[counter].Controls)
{
而在這裏,我將只獲取默認的一個文本框(與它的值)。但我的文本框消失了! :(
什麼你可以建議我來這裏做什麼?
PS我不能使用predifined列,就像在aspx文件asp:TemplateField
,因爲我的表中有每一次不同的量行的,它是動態的
我該如何再次運行RowDataBound?你能否描述更詳細的PLZ? – 2010-11-09 19:57:51
您可能需要重新調整您添加這些控件的方式。我不能給你一個好的(簡單的)例子,因爲這個問題有點複雜。您必須瞭解的主要觀點是:在回發時,您負責_recreating_任何動態創建的控件,以便回發處理程序可以使用回髮結果填充它們。 – CodingGorilla 2010-11-10 13:06:35