2011-01-25 130 views
0

更新:找到了具有相同ID「'的多個控件。 FindControl需要控件具有唯一的ID

這裏是我觀察到:

if i get org.Registrations.Count = 1 then 
    txtBox.ID "_registration0_0" string 
.... 
.... 

,如果我得到org.Registrations.Count = 2,則其行爲怪異

txtBox.ID "_registration2_0" 
txtBox.ID "_registration2_1" 


after that i starts again with _registration2_0 

ps:我沒有問題,如果計數= 1

結束更新

錯誤信息清楚地表明,我想重複ID但下面是我在哪裏創建動態文本框,你看我的代碼中有什麼錯?

protected void gv_RowCreated(object sender, GridViewRowEventArgs e) 
{ 
    Students org = (namespace.Students)(e.Row.DataItem); 

    foreach (Registration reg in org.Registrations) 
    { 
     int _count = org.Registrations.Count; 
     for (int rowId = 0; rowId < _count; rowId++) 
     { 
     TextBox txtBox = new TextBox(); 
     txtBox.ID = "_registration" + e.Row.RowIndex + "_" + rowId; 
     txtBox.Text = reg.Name; 
     e.Row.Cells[7].Controls.Add(txtBox); 
    } 
    } 
} 
+0

您是否調試過代碼,它是否爲每列返回唯一的ID?我關心Row.RowIndex,這就是爲什麼我問。調試代碼並放置這些值。 – JonH 2011-01-25 19:20:00

回答

0

我不需要的foreachfor環..其預期......這就是工作爲什麼兩次執行。

foreach (Registration reg in org.Registrations) 
{ 
    // 
} 
0

看來,如果你有多個org.Registrations,你可能會得到重複的id。 看看下面的測試變量的使用:

protected void gv_RowCreated(object sender, GridViewRowEventArgs e) 
{ 
    Students org = (namespace.Students)(e.Row.DataItem); 
    int test =0; 
    foreach (Registration reg in org.Registrations) 
    { 
     test++; 
     int _count = org.Registrations.Count; 
     for (int rowId = 0; rowId < _count; rowId++) 
     { 
     TextBox txtBox = new TextBox(); 
     txtBox.ID = "_registration" + e.Row.RowIndex + "_" + rowId + "_" + test.ToString(); 
     txtBox.Text = reg.Name; 
     e.Row.Cells[7].Controls.Add(txtBox); 
    } 
    } 
} 
相關問題