refreshing second combo by first not working
-
Hi, when I select region combo, I can populate country combo but when I re-select region combo again, country combo append the listitem on top of the previous value. not sure why its happening. below is my code protected void Page_Load(object sender, EventArgs e) { if (!(IsPostBack)) fillcombos(); ddRegion.SelectedIndexChanged +=new EventHandler(ddRegion_SelectedIndexChanged); } protected void ddRegion_SelectedIndexChanged(object sender, EventArgs e) { DataSet ds = new DataSet(); if (ddRegion.SelectedIndex >-1) { ds = myBLL.GetRegionOverviewAndCountryByRegionId(Convert.ToInt32(ddRegion.SelectedValue)); DataRow dr = ds.Tables[0].Rows[0]; // get the first row returned if (dr.ItemArray.Count() > 0) { txtRegionOverview.Text = dr["RegionOverview"].ToString(); ddRegion.SelectedValue = dr["RegionId"].ToString(); } if (ds.Tables[1].Rows.Count > 0) { ddCountry.DataSource = ds.Tables[1].DefaultView; ddCountry.DataTextField = "CountryName"; ddCountry.DataValueField = "CountryCode"; } else { ddCountry.DataSource = null; } ddCountry.DataBind(); } } public void fillcombos() { List<Region> regionList = new List<Region>(); regionList = myBLL.GetRegionList(); ddRegion.DataSource = regionList; ddRegion.DataTextField = "RegionName"; ddRegion.DataValueField = "RegionId"; ddRegion.DataBind(); } --- asp:DropDownList ID="ddCountry" AutoPostBack="true" runat="server" ></asp:DropDownList> in addition, I wanna it to work when i add "select a region", "select a country" at the top with ddregion.items.insert(0,"select.." please help
-
Hi, when I select region combo, I can populate country combo but when I re-select region combo again, country combo append the listitem on top of the previous value. not sure why its happening. below is my code protected void Page_Load(object sender, EventArgs e) { if (!(IsPostBack)) fillcombos(); ddRegion.SelectedIndexChanged +=new EventHandler(ddRegion_SelectedIndexChanged); } protected void ddRegion_SelectedIndexChanged(object sender, EventArgs e) { DataSet ds = new DataSet(); if (ddRegion.SelectedIndex >-1) { ds = myBLL.GetRegionOverviewAndCountryByRegionId(Convert.ToInt32(ddRegion.SelectedValue)); DataRow dr = ds.Tables[0].Rows[0]; // get the first row returned if (dr.ItemArray.Count() > 0) { txtRegionOverview.Text = dr["RegionOverview"].ToString(); ddRegion.SelectedValue = dr["RegionId"].ToString(); } if (ds.Tables[1].Rows.Count > 0) { ddCountry.DataSource = ds.Tables[1].DefaultView; ddCountry.DataTextField = "CountryName"; ddCountry.DataValueField = "CountryCode"; } else { ddCountry.DataSource = null; } ddCountry.DataBind(); } } public void fillcombos() { List<Region> regionList = new List<Region>(); regionList = myBLL.GetRegionList(); ddRegion.DataSource = regionList; ddRegion.DataTextField = "RegionName"; ddRegion.DataValueField = "RegionId"; ddRegion.DataBind(); } --- asp:DropDownList ID="ddCountry" AutoPostBack="true" runat="server" ></asp:DropDownList> in addition, I wanna it to work when i add "select a region", "select a country" at the top with ddregion.items.insert(0,"select.." please help
A couple of things: 0) Change your
Page_Load
method to do this:if (!IsPostBack)
{
fillcombos();
ddRegion.SelectedIndexChanged +=new EventHandler(ddRegion_SelectedIndexChanged);
}- Either clear the country combo box or check
IsPostBack
again before adding stuff to it. 2) Your question should have been put into the ASP.Net forum. 3) Learn how to use the <PRE> tag here for code snippets.
".45 ACP - because shooting twice is just silly" - JSOP, 2010
-----
You can never have too much ammo - unless you're swimming, or on fire. - JSOP, 2010
-----
"Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass." - Dale Earnhardt, 1997 - Either clear the country combo box or check
-
A couple of things: 0) Change your
Page_Load
method to do this:if (!IsPostBack)
{
fillcombos();
ddRegion.SelectedIndexChanged +=new EventHandler(ddRegion_SelectedIndexChanged);
}- Either clear the country combo box or check
IsPostBack
again before adding stuff to it. 2) Your question should have been put into the ASP.Net forum. 3) Learn how to use the <PRE> tag here for code snippets.
".45 ACP - because shooting twice is just silly" - JSOP, 2010
-----
You can never have too much ammo - unless you're swimming, or on fire. - JSOP, 2010
-----
"Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass." - Dale Earnhardt, 1997 - Either clear the country combo box or check
-
Ok I had to clear the combo in pageload but how do I add "Please select" in this situation? -- Modified Wednesday, April 13, 2011 9:02 AM
if this is in fact an asp.net application that you are just using c# for the code behind.. in the properties window look for these things
Items (collection) set the first item in that collection to "Please Select" or whatever you wish
AppendDataBoundItems set to true
ViewStateMode = false
Go to the first box and set to AutoPostBack = true.
Programming is a race between programmers trying to build bigger and better idiot proof programs, and the universe trying to build bigger and better idiots, so far... the universe is winning.