locked
Setting Default Value in DropDown RRS feed

  • Question

  • User605947588 posted

    hi, i have a country dropdownlist (asp.net server side control) where in i wish to set United States as first country from list of alphabetical countries in drop down. How should i do that?

    Plus on selection of country the respective state gets fired up. So if USA is selected in country by default, the states need to be selected by default (Of USA of course!!!)

    Below is the code of country dropdown.

    Public Sub GetCountry(ByVal ddl As DropDownList)
                Dim dt As New DataTable
                Dim con As New SqlConnection
                Dim da As New SqlDataAdapter
                Try

                    con.ConnectionString = ConfigurationManager.AppSettings("SiteSqlServer")
                    con.Open()
                    Dim strQuery As String = "Select countryid,name from tablename order by countryname"
                    Dim cmd As New SqlCommand(strQuery, con)

                    da.SelectCommand = cmd
                    da.Fill(dt)
                    If Not dt Is Nothing AndAlso dt.Rows.Count > 0 Then
                        ddl.DataTextField = "countryid"
                        ddl.DataValueField = "name"
                        ddl.DataSource = dt
                        ddl.DataBind()
                    End If
                    da.Dispose()
                    dt.Dispose()
                    cmd.Dispose()
                    con.Close()
                    con.Dispose()
                Catch ex As Exception
                End Try
            End Sub

    Thursday, January 28, 2010 1:29 AM

Answers

  • User1550595525 posted

    Hi,You can Do this in C# as follows:

    ddl.SelectedIndex = ddl.Items.IndexOf(ddl.Items.FindByText("Country Name"));

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, January 28, 2010 2:11 AM

All replies

  • User485493498 posted

    After Binding your dropdown you can select the USA seleccted by default as following.

    ddl.DataSource = dt;

    ddl.DataBind();

    ddl.SelectedValue = "USA";

    And after that you need to bind the Stats dropdown by writing a function let's say PopulateStatsByCountry. And it needs one parameter which obivously country name.

    private void PopulateStatsByCountry(string country)

    And in that function you will run the query to get the states of that country which is passed to this function as a parameter.

    And you will bind the stats dropdown as you bind the country dropdown but obivously the datasource will be the stats.

    I hope it may help you.

    Regards,

    Imran Ahmad Mughal

    Thursday, January 28, 2010 1:56 AM
  • User-1552646770 posted

    Hi,

     

    After assigning datasource to dropdown list, write following code.

    ddl.SelectedItem.text ="USA"; (if you know the name).

     

    or

    ddl.SelectedValue ="01"; (if you know the CountryID of USA).


    After Assigning the Id or Name of the Country you can call "SelectedIndexChanged" event of Country dropdown and from that you can bind your state dropdown with the selected value of the country dropdown.

    like here.....

    this code is place after dropdown was fill with value.

    ddl.SelectedValue ="01"; (if you know the CountryID of USA).

    ddl_SelectedIndexchanged(sender,e);

     

    and in selected index changed event you can write your code for binding state.

     

     

     

     

     

    Thursday, January 28, 2010 2:03 AM
  • User1550595525 posted

    Hi,You can Do this in C# as follows:

    ddl.SelectedIndex = ddl.Items.IndexOf(ddl.Items.FindByText("Country Name"));

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, January 28, 2010 2:11 AM
  • User605947588 posted

    I tried with 2 methods.

    a. SelectedValue doesn't worked out.

    b. SelectedText does work out but doesn't fire up the State Function. It Just stands as USA doesn't populate the State Function


    thanks

    Thursday, January 28, 2010 4:07 AM
  • User485493498 posted

    You need to make sure that the AutoPostBack property of dropdown is set to true. if it's set to false then the postback will not occur and you did not populate the stats dropdown.


    I hope it may help you.

    Regards,

    Imran Ahmad Mughal

    Thursday, January 28, 2010 8:32 AM
  • User-1701445313 posted

    b. SelectedText does work out but doesn't fire up the State Function. It Just stands as USA doesn't populate the State Function

     

     Hi,

    If you know for which country you want to invoke state function, you dont need to fire that function automatically. You can fire that function just after populating countries and selecting text. like below

    a. populate country dropdown

    b. dropdownlist1.text = "usa";

    c. call function PopulateStates("ID_Of_USA")

     

     

    Thursday, January 28, 2010 9:23 AM
  • User605947588 posted

    thanks for your input, but dropdown autopostback is set to true. 

    Friday, January 29, 2010 12:37 AM