Karthik
Karthik

Reputation: 2399

bind many dropdownlist in a continous fashion

Hi all i need to bind a list of dropdown to values in database.Each drop down will be inside a panel and i have named it as ddlxx1,ddlxx2,ddlxx3 in a continuous fashion. all these drop down list will have the same data source.Is there any way to bind these control in a loop or should i find control each time which is in panel then bind it?

Something like:

            for(int i=1;i<=10;i++)
            {
            ddlxx+"i".DataSource = Prod.GetValues();
            ddlxx+"i".DataTextField = "ComponentID";
            ddlxx+"i".DataValueField = "ComponentName";
            ddlxx+"i".DataBind();
            }

Please help

Upvotes: 0

Views: 65

Answers (2)

hutchonoid
hutchonoid

Reputation: 33306

If your drop down lists are added to the mark-up you can simply do the following:

protected void Page_Load(object sender, EventArgs e)
{
    if (!this.IsPostBack)
    {
       for(int i=1;i<=10;i++)
        {
          var ddl =  FindControl("ddlxx" + i) as DropDownList;
          if (ddl != null)
          {
            BindDropDown(ddl);
          }
        }
    }
}

private void BindDropDown(DropDownDataList ddl) 
{
   ddl.DataSource = Prod.GetValues();
   ddl.DataTextField = "ComponentID";
   ddl.DataValueField = "ComponentName";
   ddl.DataBind();
}

Upvotes: 1

sangram parmar
sangram parmar

Reputation: 8726

Try this:

        for (int i = 1; i <= 10; i++)
        {
            DropDownList drp = (DropDownList)panel1.FindControl("ddlxx" + i.ToString());//panel1 is your panel which contain dropdown ddlxx1,ddlxx2,ddlxx3...
            drp.DataSource = Prod.GetComponents();
            drp.DataTextField = "ComponentID";
            drp.DataValueField = "ComponentName";
            drp.DataBind();
        }

Upvotes: 0

Related Questions