2014-01-22 41 views
0

我將下拉列表添加到我的頁面上,具體取決於數據庫條目的數量,當我按下按鈕時,我想要在每個下拉列表中獲取選定的值。找到動態添加的控件

我想這

foreach(DropDownList a in Form.Controls.OfType<DropDownList>()) 
{ 
    Response.Write(a.SelectedValue); 
} 

,但沒有找到任何網頁上的下拉列表。以下是我用來添加dorpdownlists的代碼。

protected void Page_Init() 
{ 
    string product = Request.QueryString["product"]; 
    foreach (productoption r in dbcon.GetOption(product)) 
    { 
     TableRow row = new TableRow(); 
     TableCell cel1 = new TableCell(); 
     TableCell cel2 = new TableCell(); 
     DropDownList dropdown1 = new DropDownList(); 
     dropdown1.CssClass = "productdropdown"; 
     foreach (suboption f in dbcon.GetSubOption(r.ProductOptionID)) 
     { 
      dropdown1.Items.Add(f.SubOptionName + " +$" +f.SubOptionPrice); 
     } 
     cel1.Text = "<b>" + r.OptionName + "</b>"; 
     cel2.Controls.Add(dropdown1); 
     row.Cells.Add(cel1); 
     row.Cells.Add(cel2); 
     Table1.Rows.Add(row); 
    } 
    TableRow row2 = new TableRow(); 
    TableCell cell3 = new TableCell(); 
    Button cartbutton = new Button(); 
    cartbutton.ID = product; 
    cartbutton.CssClass = "btn_addcart"; 
    cartbutton.Click += cartbutton_OnClick; 
    cartbutton.Text = "Add to cart"; 
    cell3.Controls.Add(cartbutton); 
    row2.Cells.Add(cell3); 
    Table1.Rows.Add(row2); 
} 
+1

未找到page_init中的Form.Controls.Add(dropdown1)。你在哪裏寫代碼來在你的頁面上添加'DropDownList'。 – Sameer

+0

我總是發現,當創建動態控件(在運行時創建)時,稍後引用這些控件的最佳方式是創建它們時,將它們添加到私有本地字典中,然後將控件的名稱設置爲最不瞭解的模式,以便您可以使用它作爲關鍵。如果要創建同一類型的多個控件,請使用for循環並使用String.Format(「{0} {1}」,「controlType」,loopVariable)。 –

+0

@Sameer在page_init視圖狀態下的好回答尚未加載。 – Liran

回答

0
foreach (TabelRow row in Table1.Rows) 
{ 
    if(row.Cells.Count > 0) 
    { 
     if (row.Cells[1].Controls.Count > 0 && row.Cells[1].Controls[0].GetType() == typeof(DropDownList)) 
     { 
      Response.Write(a.SelectedValue); 
     } 
    } 
} 
0

首先,您應該製作一個函數,查找ControlCollection中的控件類型並返回找到的控件列表。類似的東西:

public List<T> GetControlsOfType<T>(ControlCollection controls) 
    { 
     List<T> ret = new List<T>(); 
     try 
     { 
      foreach (Control control in controls) 
      {    
        if (control is T) 
         ret.Add((T)((object)control));        
        else if (control.Controls.Count > 0) 
         ret.AddRange(GetControlsOfType<T>(control.Controls));     
      } 
     } 
     catch (Exception ex) 
     { 
      //Log the exception 
     } 
     return ret; 
    } 

,然後你可以得到所有DropDownList這樣的:

List<DropDownList> ret = GetControlsOfType<DropDownList>(this.Page.Controls); 

我希望它幫助。

0

You should be adding controls inside another control for example a panel * Also you dont need to define controls at page init, you can do that at page load and they will retain their value *

protected void Page_Load(object sender, EventArgs e) 
{ 
    loadControls(); 
} 

//對於實例讓我們一個下拉列表,並將其添加到名爲パ面板

Protected void loadControls() 
{ 
    DropdownList ddlDynamic = new DropdownList(); 
    //give this control an id 
    ddlDynamic.Id = "ddlDynamic1"; // this id is very important as the control can be found with same id 
    //add data to dropdownlist 
    //adding to the panel 
    testpanel.Controls.Add(ddlDynamic); 
} 

//現在我們必須找回這個控制回發例如butto n點擊

protected void btnPreviousSet_Click(object sender, EventArgs e) 
    { 
     //this will find the control here 
     //we will you the same id used while creating control 
     DropdownList ddlDynamic1 = testpanel.FindControl("ddlDynamic1") as DropdownList; 
     //can resume your operation here 
    }