Get Value From Something Other Than The Index In DropDownList

Discussion in 'ASP .Net' started by Cindy, Aug 29, 2007.

  1. Cindy

    Cindy Guest

    Hi.

    Say a the query behind a dropdown list lists various fields -- i.e.,
    ID, State, City. The dropdownlist will display the state and store
    the ID. How do I retrieve the associated city?

    Thanks!

    Cindy
    Cindy, Aug 29, 2007
    #1
    1. Advertising

  2. Your query will result in populating some object (data source) with data
    and the ddl will databind to that object. You will need to persist the data
    source between postbacks. You can do it in a session variable. Or you may
    choose running the query on every postback to re-populate the data source.
    Once you know the selected id, you can search your data source to locate the
    data item and get the city from the data item.

    --
    Eliyahu Goldin,
    Software Developer
    Microsoft MVP [ASP.NET]
    http://msmvps.com/blogs/egoldin
    http://usableasp.net


    "Cindy" <> wrote in message
    news:...
    > Hi.
    >
    > Say a the query behind a dropdown list lists various fields -- i.e.,
    > ID, State, City. The dropdownlist will display the state and store
    > the ID. How do I retrieve the associated city?
    >
    > Thanks!
    >
    > Cindy
    >
    Eliyahu Goldin, Aug 29, 2007
    #2
    1. Advertising

  3. "Cindy" <> wrote in message
    news:...

    > Say a the query behind a dropdown list lists various fields -- i.e.,
    > ID, State, City. The dropdownlist will display the state and store
    > the ID. How do I retrieve the associated city?


    Several ways...

    1) Unless your DataSet is quite large, you can store it in a client-side
    JavaScript array - this would allow you to retrieve the associated city
    client-side without any postback / callback...

    2) Concatenate the ID and city with a separator charactor and use that as
    the value of the dropdown e.g.

    1¬London
    2¬Paris
    3¬New York

    Then, when you need to, you can simply split the value on your character
    separator e.g.

    MyDropDown.SelectedValue.Split('¬')[1];

    3) Use AJAX to either look the data up from the database as and when
    required or, (maybe) more efficiently, persist the data in Session or
    Application cache if it's unlikely to change very often...

    I'd go for the third option, personally...


    --
    Mark Rae
    ASP.NET MVP
    http://www.markrae.net
    Mark Rae [MVP], Aug 29, 2007
    #3
  4. Cindy

    Cindy Guest

    Hey. Do you have an example of this? All I wanted was to be able to
    say, whatever=dropdownlist.column(2) and it returns the information.
    Simple. I do not have enough experience to understand everything you
    just wrote.

    Thanks!

    Cindy

    On Aug 29, 10:49 am, "Eliyahu Goldin"
    <> wrote:
    > Your query will result in populating some object (data source) with data
    > and the ddl will databind to that object. You will need to persist the data
    > source between postbacks. You can do it in a session variable. Or you may
    > choose running the query on every postback to re-populate the data source.
    > Once you know the selected id, you can search your data source to locate the
    > data item and get the city from the data item.
    >
    > --
    > Eliyahu Goldin,
    > Software Developer
    > Microsoft MVP [ASP.NET]http://msmvps.com/blogs/egoldinhttp://usableasp.net
    >
    > "Cindy" <> wrote in message
    >
    > news:...
    >
    >
    >
    > > Hi.

    >
    > > Say a the query behind a dropdown list lists various fields -- i.e.,
    > > ID, State, City. The dropdownlist will display the state and store
    > > the ID. How do I retrieve the associated city?

    >
    > > Thanks!

    >
    > > Cindy- Hide quoted text -

    >
    > - Show quoted text -
    Cindy, Aug 29, 2007
    #4
  5. On Aug 29, 10:19 am, Cindy <> wrote:
    > Hi.
    >
    > Say a the query behind a dropdown list lists various fields -- i.e.,
    > ID, State, City. The dropdownlist will display the state and store
    > the ID. How do I retrieve the associated city?
    >
    > Thanks!
    >
    > Cindy


    string stateid = StateList.SelectedItem.Value;
    string sql = "SELECT cityid, city FROM cities WHERE stateid='" +
    stateid + "'";
    Alexey Smirnov, Aug 29, 2007
    #5
  6. Cindy

    Cindy Guest

    On Aug 29, 10:53 am, "Mark Rae [MVP]" <> wrote:
    > "Cindy" <> wrote in message
    >
    > news:...
    >
    > > Say a the query behind a dropdown list lists various fields -- i.e.,
    > > ID, State, City. The dropdownlist will display the state and store
    > > the ID. How do I retrieve the associated city?

    >
    > Several ways...
    >
    > 1) Unless your DataSet is quite large, you can store it in a client-side
    > JavaScript array - this would allow you to retrieve the associated city
    > client-side without any postback / callback...
    >
    > 2) Concatenate the ID and city with a separator charactor and use that as
    > the value of the dropdown e.g.
    >
    > 1¬London
    > 2¬Paris
    > 3¬New York
    >
    > Then, when you need to, you can simply split the value on your character
    > separator e.g.
    >
    > MyDropDown.SelectedValue.Split('¬')[1];
    >
    > 3) Use AJAX to either look the data up from the database as and when
    > required or, (maybe) more efficiently, persist the data in Session or
    > Application cache if it's unlikely to change very often...
    >
    > I'd go for the third option, personally...
    >
    > --
    > Mark Rae
    > ASP.NET MVPhttp://www.markrae.net


    Thanks! The splitting ought to do it!
    Cindy, Aug 29, 2007
    #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. Andy Fish
    Replies:
    2
    Views:
    3,074
    Andy Fish
    Aug 11, 2004
  2. =?Utf-8?B?QnJpYW4gUGVhcnNvbg==?=

    use something other than "\bin" for local assemblies

    =?Utf-8?B?QnJpYW4gUGVhcnNvbg==?=, Jan 13, 2005, in forum: ASP .Net
    Replies:
    4
    Views:
    1,855
    Scott Allen
    Jan 14, 2005
  3. darrel
    Replies:
    5
    Views:
    3,645
    Lucas Tam
    May 6, 2005
  4. Steve Sobol
    Replies:
    3
    Views:
    2,151
    Steve Sobol
    Sep 20, 2004
  5. Tomasz Chmielewski

    sorting index-15, index-9, index-110 "the human way"?

    Tomasz Chmielewski, Mar 4, 2008, in forum: Perl Misc
    Replies:
    4
    Views:
    279
    Tomasz Chmielewski
    Mar 4, 2008
Loading...

Share This Page