Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New to the CF scene
    Join Date
    Apr 2011
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Dynamic drop down list by using asp.net, c# and sql server

    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 .

  • #2
    Regular Coder
    Join Date
    Apr 2009
    Posts
    244
    Thanks
    1
    Thanked 20 Times in 20 Posts
    Hi preetpalkapoor,

    If you're asking how to bind data, here is an example:
    http://www.dotnetheaven.com/UploadFi...ndatagrid.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:

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

    Code:
    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


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •