2011-07-10 71 views
1

我有一個簡單的頁面,1個PlaceHolder和2個按鈕(「添加行」,「獲取價值」),沒有別的。當我點擊添加行按鈕I創建一個動態表,並將一些控制在它與集ClientIDMoode電池=靜:如何訪問控制動態添加到頁面與靜態ClientIDMode

private void CreateTableWithSession(bool FirstLoad) 
{ 
    Table tbl; 
    int Row; 
    if (Session["tbl"]==null) 
    { 
     tbl = new Table(); 
    } 
    else 
    { 
     tbl = (Table)Session["tbl"]; 
    } 
    if (FirstLoad == true) 
    { 
     tbl.Rows.Clear(); 
    } 
    ////////////////////////////////////////////////// 
    if (Session["Row"] == null) 
    { 
     Row = 0; 
    } 
    else 
    { 
     Row = int.Parse(Session["Row"].ToString()); 
    } 
    if (FirstLoad == true) 
    { 
     tbl.Rows.Clear(); 
    } 
    ////////////////////////////////////////////////// 

    TableRow tr1 = new TableRow(); 
    TableCell tc = new TableCell(); 

    TextBox txtBoxUserName = new TextBox(); 
    txtBoxUserName.ClientIDMode = System.Web.UI.ClientIDMode.Static; 
    txtBoxUserName.Text = "0"; 
    Col++; 
    txtBoxUserName.ID = "RowNo" + Row.ToString() + "ColumnNo" + Col.ToString(); 
    tc.Controls.Add(txtBoxUserName); 
    tc.Width = 200; 
    tc.VerticalAlign = VerticalAlign.Middle; 
    tc.HorizontalAlign = HorizontalAlign.Center; 
    tr1.Cells.Add(tc); 

    tc = new TableCell(); 
    TextBox txtBoxPassword = new TextBox(); 
    txtBoxPassword.ClientIDMode = System.Web.UI.ClientIDMode.Static; 
    txtBoxPassword.Text = "0"; 
    Col++; 
    txtBoxPassword.ID = "RowNo" + Row.ToString() + "ColumnNo" + Col.ToString(); 
    tc.Controls.Add(txtBoxPassword); 
    tc.Width = 200; 
    tc.VerticalAlign = VerticalAlign.Middle; 
    tc.HorizontalAlign = HorizontalAlign.Center; 
    tr1.Cells.Add(tc); 

    tbl.Rows.Add(tr1); 

    Session["tbl"] = tbl; 

    PlaceHolder1.Controls.Add(tbl); 
} 

,我希望當上「獲得值」按鈕,用戶點擊,得到2倍文本框的值(即動態增加),但每一次它返回Null。

TextBox txtBoxUserName = this.FindControl("RowNo1ColumnNo1") as TextBox; 

我該如何解決這個問題,並獲得文本框的值?

感謝

+0

你可以請發佈完整的代碼,你的添加行並獲得價值按鈕的代碼? –

回答

1

動態創建的控件應該重新然後才能訪問,我相信你是不是創建控件時你打Get Value按鈕。東西會爲你工作...

protected void AddRow_Click(object sender, EventArgs e) 
{ 
    CreateTableWithSession() 
    ................... 
} 

protected void GetValue_Click(object sender, EventArgs e) 
{ 
    CreateTableWithSession() 
    TextBox txtBoxUserName = this.FindControl("RowNo1ColumnNo1") as TextBox; 
    ........................ 
} 
+0

是的。我認爲創建表必須位於page_load中,並且無法添加控件以形成表單,然後在不重新創建表的情況下訪問它。 – Arian