...

View Full Version : Dynamic drop down list by using asp.net, c# and sql server



preetpalkapoor
04-07-2011, 06:03 PM
Hi

I am trying to bind data with 2 dropdowns.

I am having 2 tables (sql server):
1)country:
country_id(primary key)
country_name
2)state:
state_id(primary key)
state_name
country_id(foreign_key)

now i tried to bind the data with 2 dropdown list
ddp1 and ddp2.
such that when i select the country from first dropdown(ddp1) then the state corresponding to that country should come in the second dropdown(ddp2).

Can some one please help me .

Thanks .

Mike_O
04-08-2011, 04:04 PM
Hi preetpalkapoor,

If you're asking how to bind data, here is an example:
http://www.dotnetheaven.com/UploadFile/rahul4_saxena/dropdownlisiindatagrid05082007024918AM/dropdownlisiindatagrid.aspx

Now, if you're asking when to bind data in your second dropdown, well, you would do that in the event of making a selection in your first dropdown. I think OnSelectedIndexChanged should do it. So, your first dropdown will look something like this:


<asp:DropDownList ID="ddp1" runat="server" ... OnSelectedIndexChanged="GetStates" ...>
...
</asp:DropdownList>
Then in your codebehind, you will have method:


protected void GetStates(object sender, EventArgs e)
{
//Get states from SQL
//Bind results to ddp2
}


Easy. Oh, and one tip from me (since you happen to be using C#): Use the "Using" statement to create your objects (the disposable ones, that is)

Regards,
Mike



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum