【问题标题】:find dynamically added controls查找动态添加的控件
【发布时间】:2014-01-22 16:49:19
【问题描述】:

我根据数据库条目的数量向我的页面添加下拉列表,当我按下按钮时,我想在每个下拉列表中获取选定的值。

我试过了

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);
}

【问题讨论】:

  • 在 page_init 中未找到 Form.Controls.Add(dropdown1)。你在哪里写代码在你的页面上添加DropDownList
  • 我一直发现,在创建动态控件时(在运行时创建),以后引用这些控件的最好方法是在创建它们的时候,将它们添加到私有本地字典中,然后将其命名为控制一些你至少会知道模式的东西,这样你就可以用它作为关键。如果您要创建多个相同类型的控件,请使用 for 循环并使用 String.Format("{0}{1}", "controlType", loopVariable)。
  • @Sameer 在 page_init 视图状态下的好答案尚未加载。
  • @Sameer @Liran 在循环里面有一个 Table1.Rows.Add(row);可能是&lt;asp:Table /&gt; 放置在.aspx 页面上的&lt;form&gt;&lt;/form&gt; 标记之间。
  • 试试这个可能有用foreach (TabelRow row in Table1.Rows) { DropDownList a = (DropDownList)row.Cells[1].Controls[0]; Response.Write(a.SelectedValue); }

标签: c# asp.net c#-4.0 dynamic


【解决方案1】:
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); 
        }
    }
}

【讨论】:

    【解决方案2】:

    首先,您应该创建一个函数,在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);
    

    希望对你有所帮助。

    【讨论】:

      【解决方案3】:

      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();
       }
      

      //例如,让我们获取一个下拉列表并将其添加到名为 testpanel 的面板中

      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);
      }
      

      //现在我们必须在回发时找到这个控件,例如单击按钮

      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
           }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-07-15
        • 2012-11-27
        • 1970-01-01
        • 2012-11-08
        • 1970-01-01
        相关资源
        最近更新 更多