preetpal kapoor
preetpal kapoor

Reputation: 31

Dynamic dropdown list in asp.net and c#

I am trying to bind data with 2 dropdowns.

I am have 2 tables created in SQL Server 2008:

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 list of states corresponding to that country should come in the second dropdown(ddp2).

Can some one please help me to achieve this?

Thanks .

Upvotes: 3

Views: 3909

Answers (4)

Muhammad Akhtar
Muhammad Akhtar

Reputation: 52241

Best and easy way

<asp:DropDownList ID="ddlCountry" runat="server" DataSourceID="odsCountry"              
    DataTextField="CountryName" DataValueField="CountryID" AutoPostBack="True">                                              </asp:DropDownList>                                                         
<asp:ObjectDataSource ID="odsCountry" runat="server" SelectMethod="GetAllCountry"
TypeName=""></asp:ObjectDataSource>


<asp:DropDownList ID="ddlRegion" runat="server" DataSourceID="odsRegion"                                                          DataTextField="StateName" DataValueField="StateID">                                                                     </asp:DropDownList>
<asp:ObjectDataSource ID="odsRegion" runat="server" SelectMethod="GetStateByCountryID"
     TypeName="Core.BLL.State">
     <SelectParameters>
<asp:ControlParameter ControlID="ddlCountry" Name="countryID" 
PropertyName="SelectedValue" Type="Int32" />                                                                
     </SelectParameters>
</asp:ObjectDataSource>

Upvotes: 1

iandayman
iandayman

Reputation: 4467

Making a huge amount of assumptions but make sure ddp1.textdatafield = "country_name" and ddp1.valuedatafield = "country_id" make sure ddp2.textdatafield = "state_name" and ddp2.valuedatafield = "state_id"

Wire an indexchanged event to ddp1. Your selectedindexchanged event on ddp1 should call a stored proc / query to return all rows from the state table where country_id(foreign_key) = the ddp1.selectedindex value and use this as the datasource to the second dd list.

I think you can auto-wire this when using a sql/objectdatasource control by specifying the input parameter to the second dd list as the selectedvalue of the first dd list.

Upvotes: 0

Andy Rose
Andy Rose

Reputation: 16974

You won't be able to bind the 'state' to ddp2 until you know what 'country' has been selected in ddp1.

To achieve what you want you could use JavaScript, ideally jQuery, to populate the second drop down list when a value has been selected in the first. This could be done by creating arrays on the client holding all the data or performing an ajax call back to the server to get the state data.

Another option would be to perform an 'autopostback' to the server to populate the second drop down list when the first has been changed.

Upvotes: 0

Becuzz
Becuzz

Reputation: 6866

You can use update panels to make this happen.

1) Put both controls in an update panel. 2) Set the autopostback property of the the first drop down to true. 3) Add a trigger to the update panel for the drop down list, using EventName="SelectedIndexChanged" 4) Then in the code behind have a function to handle the SelectedIndexChanged event for the first drop down. In that function, bind the data for the second drop down.

It will look roughly like this (note: not tested)

<asp:UpdatePanel runat="server" ID="update1" UpdateMode="Conditional">
    <asp:Dropdownlist runat="server" ID="firstddl" AutoPostBack="True"/>
    <asp:Dropdownlist runat="server" ID="secondddl" />
    <Triggers>
        <asp:AsyncPostBackTrigger ControlID="firstddl" EventName="SelectedIndexChanged" />
    </Triggers>
</asp:UpdatePanel>

Code behind (again not tested)

Protected Sub firstddl_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles firstddl.SelectedIndexChanged
    'Update secondddl here
End Sub

Upvotes: 1

Related Questions