2014-01-10 97 views
0

在我的網站,我可以創建動態使用更新面板控制控件(文本框下拉)。但我無法訪問這些控件。即使在調試期間在瀏覽器中查看源代碼時,動態創建的控件也不存在!我想知道我們如何能夠看到動態創建的控件,而在o/p的源代碼視圖中沒有定義它們的定義。你能否清楚我的這個疑問。非常感謝。無法訪問動態創建的控件

代碼:

​​

和相應的代碼背後,是

 protected void Page_Load(object sender, EventArgs e) 
    { 
     int intRowCount = Convert.ToInt32(ViewState["No_of_Rows"]); 
     for (int i = 1; i <= intRowCount; i++) 
     { 
      TableRow row = new TableRow(); 
      TableCell cell1 = new TableCell(); 
      TableCell cell2 = new TableCell(); 
      TableCell cell3 = new TableCell(); 
      TableCell cell4 = new TableCell(); 
      TextBox tb = new TextBox(); 
      DropDownList ddl = new DropDownList(); 
      Label lbl1 = new Label(); 
      Label lbl2 = new Label(); 

      row.ID = "rwAssocIncRec" + i; 

      cell1.ID = "cAssocIncRec" + i + "1"; 
      cell2.ID = "cAssocIncRec" + i + "2"; 
      cell3.ID = "cAssocIncRec" + i + "3"; 
      cell4.ID = "cAssocIncRec" + i + "4"; 
      cell2.Attributes.Add("align", "left"); 
      cell4.Attributes.Add("align", "left"); 


      tb.ID = "txtAssocIncRec" + i; 
      tb.MaxLength = 12; 
      tb.EnableViewState = true; 

      lbl1.ID = "lblAssocIncRec" + i + "01"; 
      lbl2.ID = "lblAssocIncRec" + i + "02"; 

      ddl.ID = "ddlbAssocIncRec" + i; 

      cell1.Controls.Add(lbl1); 
      cell2.Controls.Add(tb); 
      cell3.Controls.Add(lbl2); 
      cell4.Controls.Add(ddl); 
      ddl.Items.Add(new ListItem("--Select--", "0")); 
      ddl.Items.Add(new ListItem("1", "1")); 
      ddl.Items.Add(new ListItem("2", "2")); 


      row.Cells.Add(cell1); 
      row.Cells.Add(cell2); 
      row.Cells.Add(cell3); 
      row.Cells.Add(cell4); 

      someTBL.Rows.Add(row); 
     } 

    } 
    protected void btnAddNewRowAssocInc_Click(object sender, EventArgs e) 
    { 

     TableRow newRow = new TableRow(); 
     newRow.ID = "trAssocIncRec" + lblcount.Text; 

     TableCell newCell1 = new TableCell(); 
     newCell1.ID = "tdAssocIncRec1" + lblcount.Text; 
     Label lbl = new Label(); 
     lbl.Text = ""; 
     lbl.ID = "lblAssocIncRec" + lblcount.Text; 
     newCell1.Controls.Add(lbl); 
     newRow.Cells.Add(newCell1); 

     TableCell newCell2 = new TableCell(); 
     newCell2.ID = "tdAssocIncRec2" + lblcount.Text; 
     TextBox txtBox = new TextBox(); 
     txtBox.ID = "txtAssocIncRec" + lblcount.Text; 
     txtBox.MaxLength = 12; 
     newCell2.Attributes.Add("align", "left"); 
     newCell2.Controls.Add(txtBox); 
     newRow.Cells.Add(newCell2); 

     TableCell newCell3 = new TableCell(); 
     newCell3.ID = "tdAssocIncRec3" + lblcount.Text; 
     Label lbl2 = new Label(); 
     lbl2.Text = ""; 
     lbl2.ID = "lblAssocIncRec2" + lblcount.Text; 
     newCell3.Controls.Add(lbl2); 
     newRow.Cells.Add(newCell3); 

     TableCell newCell4 = new TableCell(); 
     newCell4.ID = "tdAssocIncRec4" + lblcount.Text; 
     DropDownList ddlb = new DropDownList(); 
     ddlb.ID = "ddlbAssocIncRec" + lblcount.Text; 
     newCell4.Attributes.Add("align", "left"); 
     newCell4.Controls.Add(ddlb); 
     ddlb.Items.Add(new ListItem("--Select--", "0")); 
     ddlb.Items.Add(new ListItem("1", "1")); 
     ddlb.Items.Add(new ListItem("2", "2")); 
     newRow.Cells.Add(newCell4); 

     someTBL.Rows.Add(newRow); 
     ViewState["No_of_Rows"] = Convert.ToInt32(lblcount.Text); 

     lblcount.Text = Convert.ToString(Convert.ToInt32(lblcount.Text) + 1); 


    } 
+0

看看生命週期:http://msdn.microsoft.com/en-us/library/ms178472.aspx –

回答

0

你需要做的是在任何負載情況下,或在頁面的每次初始化事件重新創建這些。這樣可以檢索值。

然後,我會找到你所創建的控制和他們在一個視圖狀態添加到列表中(因此它被當用戶離開頁面毀)

ViewState["DynamicControls"] = list; 

那麼你已經存儲的值,你可以回來。

+0

嗨菲利普,感謝您的迴應。我已經在頁面加載事件中重新創建了它們。而且一切工作正常。但我無法訪問它們。例如,如果在您的PC上運行代碼,然後單擊瀏覽器輸出並單擊查看源代碼,則無法看到動態創建的控件。請你幫忙。 –

+0

您是否在視圖狀態下創建了它們? –