2014-01-22 46 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 
    }