Question: Dropdown List Control

Discussion in 'ASP .Net' started by =?Utf-8?B?TWFubnkgQ2hvaGFu?=, Mar 18, 2005.

  1. Hi Guys, I have a table with country field for each user. Now when i go into
    modify mode, i load the dropdown list with all the countries from the
    datbase. I need to preselect the index for the appropriate country assigned
    for that user. Lets say if i set Tom for Canada, and when i bring his record
    in modify mode, i should see canada instead of first county in the drop down
    list.

    Can this be done?

    Thanks in advane.

    Manny
     
    =?Utf-8?B?TWFubnkgQ2hvaGFu?=, Mar 18, 2005
    #1
    1. Advertising

  2. =?Utf-8?B?TWFubnkgQ2hvaGFu?=

    Karl Seguin Guest

    yourDropDownList.SelectedIndex =
    yourDropDownList.IndexOf(yourDropDownList.Items.FindByValue("CanadasValue"))

    without much code it's hard to help you any further...but it's something
    like that...."CanadasValue" would be the value part of the item bound to the
    dropdown..

    Karl

    --
    MY ASP.Net tutorials
    http://www.openmymind.net/ - New and Improved (yes, the popup is annoying)
    http://www.openmymind.net/faq.aspx - unofficial newsgroup FAQ (more to
    come!)


    "Manny Chohan" <> wrote in message
    news:...
    > Hi Guys, I have a table with country field for each user. Now when i go

    into
    > modify mode, i load the dropdown list with all the countries from the
    > datbase. I need to preselect the index for the appropriate country

    assigned
    > for that user. Lets say if i set Tom for Canada, and when i bring his

    record
    > in modify mode, i should see canada instead of first county in the drop

    down
    > list.
    >
    > Can this be done?
    >
    > Thanks in advane.
    >
    > Manny
     
    Karl Seguin, Mar 18, 2005
    #2
    1. Advertising

  3. On Fri, 18 Mar 2005 13:33:03 -0600, Manny Chohan
    <> wrote:

    > Hi Guys, I have a table with country field for each user. Now when i go
    > into
    > modify mode, i load the dropdown list with all the countries from the
    > datbase. I need to preselect the index for the appropriate country
    > assigned
    > for that user. Lets say if i set Tom for Canada, and when i bring his
    > record
    > in modify mode, i should see canada instead of first county in the drop
    > down
    > list.
    >
    > Can this be done?
    >
    > Thanks in advane.
    >
    > Manny


    A full example:
    http://www.dotnetjunkies.com/HowTo/B6506FDB-B5DD-4452-ABA0-37206419BD3D.dcik

    --
    Craig Deelsnyder
    Microsoft MVP - ASP/ASP.NET
     
    Craig Deelsnyder, Mar 18, 2005
    #3
  4. In the aspx page, i have
    <asp:dropdownlist id="dListOfficeCode" runat="server"
    DataTextField="Description" DataValueField="OfficeCode" />

    In Codebehind this is how i bind:

    myConnection = New SqlConnection(conStr)
    Dim myCommand As SqlCommand = New SqlCommand("Select OfficeCode
    as OfficeCode, Description as Description from OfficeCode order by
    Description", myConnection)
    Dim myDataReader As SqlDataReader
    Try
    myConnection.Open()
    myDataReader =
    myCommand.ExecuteReader(CommandBehavior.CloseConnection)
    dListOfficeCode.DataSource = myDataReader
    dListOfficeCode.DataBind()
    dListOfficeCode.Items.FindByValue(

    Catch myException As Exception
    Response.Write("An error has occurred: " &
    myException.ToString())
    Finally
    If Not myDataReader Is Nothing Then
    myDataReader.Close()
    End If

    dListOfficeCode.SelectedIndex = 0
    End Try

    Would this help?

    Manny




    "Karl Seguin" wrote:

    > yourDropDownList.SelectedIndex =
    > yourDropDownList.IndexOf(yourDropDownList.Items.FindByValue("CanadasValue"))
    >
    > without much code it's hard to help you any further...but it's something
    > like that...."CanadasValue" would be the value part of the item bound to the
    > dropdown..
    >
    > Karl
    >
    > --
    > MY ASP.Net tutorials
    > http://www.openmymind.net/ - New and Improved (yes, the popup is annoying)
    > http://www.openmymind.net/faq.aspx - unofficial newsgroup FAQ (more to
    > come!)
    >
    >
    > "Manny Chohan" <> wrote in message
    > news:...
    > > Hi Guys, I have a table with country field for each user. Now when i go

    > into
    > > modify mode, i load the dropdown list with all the countries from the
    > > datbase. I need to preselect the index for the appropriate country

    > assigned
    > > for that user. Lets say if i set Tom for Canada, and when i bring his

    > record
    > > in modify mode, i should see canada instead of first county in the drop

    > down
    > > list.
    > >
    > > Can this be done?
    > >
    > > Thanks in advane.
    > >
    > > Manny

    >
    >
    >
     
    =?Utf-8?B?TWFubnkgQ2hvaGFu?=, Mar 18, 2005
    #4
  5. =?Utf-8?B?TWFubnkgQ2hvaGFu?=

    JV Guest

    Funny. This is exactly what I was writing about (see post earlier today
    starting with "IMHO")


    "Manny Chohan" <> wrote in message
    news:...
    > Hi Guys, I have a table with country field for each user. Now when i go
    > into
    > modify mode, i load the dropdown list with all the countries from the
    > datbase. I need to preselect the index for the appropriate country
    > assigned
    > for that user. Lets say if i set Tom for Canada, and when i bring his
    > record
    > in modify mode, i should see canada instead of first county in the drop
    > down
    > list.
    >
    > Can this be done?
    >
    > Thanks in advane.
    >
    > Manny
     
    JV, Mar 18, 2005
    #5
  6. =?Utf-8?B?TWFubnkgQ2hvaGFu?=

    Karl Seguin Guest

    From what I told you, you should be able to figure it out:

    dListOfficeCode.Items.SelectedIndex =
    dListOffice.Items.IndexOf(dListOfficeCode.Items.FindByValue("canada"))

    Karl

    --
    MY ASP.Net tutorials
    http://www.openmymind.net/ - New and Improved (yes, the popup is annoying)
    http://www.openmymind.net/faq.aspx - unofficial newsgroup FAQ (more to
    come!)


    "Manny Chohan" <> wrote in message
    news:...
    > In the aspx page, i have
    > <asp:dropdownlist id="dListOfficeCode" runat="server"
    > DataTextField="Description" DataValueField="OfficeCode" />
    >
    > In Codebehind this is how i bind:
    >
    > myConnection = New SqlConnection(conStr)
    > Dim myCommand As SqlCommand = New SqlCommand("Select

    OfficeCode
    > as OfficeCode, Description as Description from OfficeCode order by
    > Description", myConnection)
    > Dim myDataReader As SqlDataReader
    > Try
    > myConnection.Open()
    > myDataReader =
    > myCommand.ExecuteReader(CommandBehavior.CloseConnection)
    > dListOfficeCode.DataSource = myDataReader
    > dListOfficeCode.DataBind()
    > dListOfficeCode.Items.FindByValue(
    >
    > Catch myException As Exception
    > Response.Write("An error has occurred: " &
    > myException.ToString())
    > Finally
    > If Not myDataReader Is Nothing Then
    > myDataReader.Close()
    > End If
    >
    > dListOfficeCode.SelectedIndex = 0
    > End Try
    >
    > Would this help?
    >
    > Manny
    >
    >
    >
    >
    > "Karl Seguin" wrote:
    >
    > > yourDropDownList.SelectedIndex =
    > >

    yourDropDownList.IndexOf(yourDropDownList.Items.FindByValue("CanadasValue"))
    > >
    > > without much code it's hard to help you any further...but it's something
    > > like that...."CanadasValue" would be the value part of the item bound to

    the
    > > dropdown..
    > >
    > > Karl
    > >
    > > --
    > > MY ASP.Net tutorials
    > > http://www.openmymind.net/ - New and Improved (yes, the popup is

    annoying)
    > > http://www.openmymind.net/faq.aspx - unofficial newsgroup FAQ (more to
    > > come!)
    > >
    > >
    > > "Manny Chohan" <> wrote in message
    > > news:...
    > > > Hi Guys, I have a table with country field for each user. Now when i

    go
    > > into
    > > > modify mode, i load the dropdown list with all the countries from the
    > > > datbase. I need to preselect the index for the appropriate country

    > > assigned
    > > > for that user. Lets say if i set Tom for Canada, and when i bring his

    > > record
    > > > in modify mode, i should see canada instead of first county in the

    drop
    > > down
    > > > list.
    > > >
    > > > Can this be done?
    > > >
    > > > Thanks in advane.
    > > >
    > > > Manny

    > >
    > >
    > >
     
    Karl Seguin, Mar 19, 2005
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. vishnu
    Replies:
    1
    Views:
    1,214
    Patrick.O.Ige
    Mar 25, 2006
  2. Mike
    Replies:
    1
    Views:
    816
  3. acadam
    Replies:
    0
    Views:
    763
    acadam
    Dec 27, 2006
  4. Aussie Rules

    dropdown list control basic question

    Aussie Rules, Jun 13, 2007, in forum: ASP .Net
    Replies:
    4
    Views:
    344
    lphufieqln
    Aug 16, 2007
  5. M. Craig
    Replies:
    1
    Views:
    750
    Victor Garcia Aprea [MVP]
    Jan 9, 2004
Loading...

Share This Page