Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

DropDownlist problem in a web part

Status
Not open for further replies.

cyrus71

Programmer
Feb 6, 2006
34
SE
Hi,
I have two dropdownlist ddl1 and ddl2 in the first time when web part is loaded ddl1 gets types from DB. (types = Region, Country, Company...) and ddl2 is not visible. When you choose one of the types (in ddl1 ), ddl2 would be visible and it gets values from db (for exampel if you choose Countries the values would be USA, Canada, ....)

The problem is ddl2 is too quick and when first time I choose a type ddl2 contains just "-- select --" nothing more but if you click on a button or any event happens then ddl2 contains the values.

I have tryed with many other events and code but the following code is nearst to my purpose.


protected override void CreateChildControls()
{
DDL1 = new DropDownList();
DDL1.ID = "DDL1";
DDL1.DataBound += new EventHandler(DDL1_DataBound);
DDL1.SelectedIndexChanged += new EventHandler(DDL1_Changed);
DDL1.DataSource = GetTypes();
DDL1.DataTextField = "type";
DDL1.DataValueField = "type";
DDL1.DataBind();
DDL1.AutoPostBack = true;
Controls.Add(DDL1);

DDL2 = new DropDownList();
DDL2.ID = "DDL2";
DDL2.Visible = false;
//DDL2.Load += new EventHandler(DDL2_Load);
DDL2.DataBound += new EventHandler(DDL2_DataBound);
DDL2.SelectedIndexChanged += new EventHandler(DDL2_Changed);
DDL2.DataSource = GetValues(DDL1.SelectedValue);
DDL2.DataTextField = "typevalue";
DDL2.DataValueField = "taxonomyid";
DDL2.DataBind();
DDL2.AutoPostBack = true;
Controls.Add(DDL2);
}
protected void DDL1_DataBound(object sender, EventArgs e)
{
DDL1.Items.Insert(0, new ListItem("-- Select Type --"));
}
protected void DDL1_Changed(object sender, EventArgs e)
{
DDL2.Visible = true;
}

protected void DDL2_DataBound(object sender, EventArgs e)
{
string ValueName = "-- Select " + DDL1.SelectedValue.ToString() + " --";
DDL2.Items.Insert(0, new ListItem(ValueName));
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top