Populating Drop Down Lists Based on Another DDL

B

Ben Arthur

Hi All,
I have 3 dropdown lists which function as filters for the data being
passed into a datagrid...On Page load, the first drop downlist populates
with relevant data(using sql query), n now, depending on the selection
of the first drop down list, corresponding values get populated into the
second drop downlist, sort of like country, state, city hierarchy...n
depending on the selected index on 2nd, the 3rd ddl gets populated, n
then the datagrid is formed with that data....I have tried placing the
code to populate the second DDL in the selectedindexchanged even of the
first DDL and it does not work as expected, i also have autopostback set
to true so that it looks dynamic to the user...this is some of the
code(ddlcountry is the first DDL

public void ddlcountry_SelectedIndexChanged(object sender,
System.EventArgs e)
{ string
connstr=ConfigurationSettings.AppSettings["ConnectionString"];
string selectext="select ....where
g.cCountryID="+ddlcountry.SelectedItem.Value;
SqlConnection con1=new SqlConnection(connstr);
Response.Write(selectext);
SqlCommand mycommand2=new SqlCommand(selectext,con1);
con1.Open();
SqlDataReader dr1= mycommand2.ExecuteReader();

ddlstate.DataSource=dr1;
ddlstate.DataTextField="state";
ddlstate.DataValueField="cStateID";
ddlsubgroup.DataBind();
con1.Close();
dr1.Close();

}

Any help will be greatly appreciated !
Thanks in advance,
Ben
 
B

Ben Arthur

OK...assume the first ddl has 2 values"USA" & Australia", when i select
USA, it populates the states correctly in the second ddl, but then if i
select australia also, as it is autopostback, it comes back to USA as
selected n so i cannot get the second DDL to populate to values for
Australia....the datagrid however does bind correctly according to the
states selected....
Please let me know if I shoudl explain better.....
Thanks for ur time
Ben
 
F

Felbrigg

Sounds like your repopulating the first dropdown in the load event. Try
wrapping the code that populates the first drop down in a
"IsPostBack==False" type check.
 
B

Ben Arthur

Yes ! Thanks, that worked....but i m not sure of how to handle
postbacks.....i.e i load the first ddl only the first time, it loads the
USA and australia, then i select one of them, it loads second ddl
accordingly and builds the datagrid,but if i select australia, it
populates the second ddl but does not bind the grid......though the
binding is not related to postback but to the selectedindexhanged(i shud
mention there is only value for the second ddl in my DB for first
ddl=australia)...is it related to the postback ...or is it because there
is only one value?
Thanks again for ur help.
Ben
 
F

Felbrigg

My usual process for this is...

Put your grid-data-binding in a method/function/sub of its own. Call this
from the page load (wrapped in a isPostBack==False) then in each of your
drop down list events you can call the grid databind method.

BTW, do you really need a grid, they are very "heavy".
 
B

Ben Arthur

thanks a lot....i had to put the binding code in postback part too n now
it works fine....

Thanks again
Ben
 
Joined
Jan 3, 2011
Messages
1
Reaction score
0
Repopulate the dropdownlist

how 2 repopulate the values of dropdownlist2 based on selected value on dropdownlist1 in asp.net
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top