Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/311.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 查找动态添加的控件_C#_Asp.net_C# 4.0_Dynamic - Fatal编程技术网

C# 查找动态添加的控件

C# 查找动态添加的控件,c#,asp.net,c#-4.0,dynamic,C#,Asp.net,C# 4.0,Dynamic,我将根据数据库条目的数量向我的页面添加dropdownlist,当我按下按钮时,我希望获得每个dropdownlist中的选定值 我试过这个 foreach(DropDownList a in Form.Controls.OfType<DropDownList>()) { Response.Write(a.SelectedValue); } foreach(DropDownList a在Form.Controls.OfType()中) { 响应。写入(a.选择的值); }

我将根据数据库条目的数量向我的页面添加dropdownlist,当我按下按钮时,我希望获得每个dropdownlist中的选定值

我试过这个

foreach(DropDownList a in Form.Controls.OfType<DropDownList>())
{
    Response.Write(a.SelectedValue);
}
foreach(DropDownList a在Form.Controls.OfType()中)
{
响应。写入(a.选择的值);
}
但在页面上找不到任何下拉列表。下面是我用来添加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);
}
protected void Page_Init()
{
字符串product=Request.QueryString[“product”];
foreach(dbcon.GetOption(product)中的productoption r)
{
TableRow行=新TableRow();
TableCell cel1=新的TableCell();
TableCell cel2=新的TableCell();
DropDownList dropdown1=新的DropDownList();
dropdown1.CssClass=“productdropdown”;
foreach(dbcon.GetSubOption(r.ProductOptionID)中的子选项f)
{
dropdown1.Items.Add(f.SubOptionName++“+$”+f.SubOptionPrice);
}
cel1.Text=”“+r.OptionName+”;
cel2.Controls.Add(下拉列表1);
行.Cells.Add(cel1);
行。单元格。添加(cel2);
表1.行。添加(行);
}
TableRow row2=新的TableRow();
TableCell cell3=新的TableCell();
按钮cartbutton=新按钮();
cartbutton.ID=产品;
cartbutton.CssClass=“btn\u addcart”;
cartbutton。单击+=cartbutton\u OnClick;
cartbutton.Text=“添加到购物车”;
单元格3.控件.添加(cartbutton);
第2行。单元格。添加(单元格3);
表1.行。添加(第2行);
}

首先,您应该创建一个函数,在
控件集合中查找控件类型,并返回找到的控件列表。诸如此类:

    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;
    }
List<DropDownList> ret = GetControlsOfType<DropDownList>(this.Page.Controls);

我希望它能有所帮助。

您应该在另一个控件中添加控件,例如面板
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); 
        }
    }
}
*
另外,您不需要在页面初始化时定义控件,您可以在页面加载时这样做,它们将保留其值
*

//例如,让我们获取dropdownlist并将其添加到名为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);
}
//现在我们必须在post back上找到此控件,例如单击按钮

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
     }

在页面初始化中找不到
Form.Controls.Add(dropdown1)
。您在何处编写代码以在页面上添加
DropDownList
。我始终发现,在创建动态控件(在运行时创建)时,以后引用这些控件的最佳方法是在创建控件时,将它们添加到专用本地字典中,然后将控件的名称设置为您至少知道模式的名称,以便您可以将其用作键。如果要创建同一类型的多个控件,请使用for循环并使用String.Format(“{0}{1}”,“controlType”,loopVariable)。@Sameer页面初始视图状态中的正确答案尚未加载。@Sameer@Liran在循环中有一个Table1.Rows.Add(row);可能是一个
放在.aspx页面上的
标记之间。试试看,它可能对每个(表1.Rows中的TabelRow行){DropDownList a=(DropDownList)row.Cells[1]。控件[0];响应。写入(a.SelectedValue);}
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
     }