Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > Get Value From Something Other Than The Index In DropDownList

Reply
Thread Tools

Get Value From Something Other Than The Index In DropDownList

 
 
Cindy
Guest
Posts: n/a
 
      08-29-2007
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

 
Reply With Quote
 
 
 
 
Eliyahu Goldin
Guest
Posts: n/a
 
      08-29-2007
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: oups.com...
> 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
>



 
Reply With Quote
 
 
 
 
Mark Rae [MVP]
Guest
Posts: n/a
 
      08-29-2007
"Cindy" <> wrote in message
news: oups.com...

> 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

 
Reply With Quote
 
Cindy
Guest
Posts: n/a
 
      08-29-2007
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"
<REMOVEALLCAPITALSeEgGoldD...@mMvVpPsS.org> 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" <cindyc...@hotmail.com> wrote in message
>
> news: oups.com...
>
>
>
> > 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 -



 
Reply With Quote
 
Alexey Smirnov
Guest
Posts: n/a
 
      08-29-2007
On Aug 29, 10:19 am, Cindy <cindyc...@hotmail.com> 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 + "'";

 
Reply With Quote
 
Cindy
Guest
Posts: n/a
 
      08-29-2007
On Aug 29, 10:53 am, "Mark Rae [MVP]" <m...@markNOSPAMrae.net> wrote:
> "Cindy" <cindyc...@hotmail.com> wrote in message
>
> news: oups.com...
>
> > 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!

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index" camelean@shaw.ca ASP .Net 3 02-22-2011 07:06 PM
value of performance index and reliability index Matt Schneyer Windows 64bit 10 11-20-2008 01:50 AM
sorting index-15, index-9, index-110 "the human way"? Tomasz Chmielewski Perl Misc 4 03-04-2008 05:01 PM
Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index Stefan ASP .Net Datagrid Control 2 07-31-2004 05:52 AM
Can I set a dropDownList to the TEXT rather than index? darrel ASP .Net 4 05-04-2004 04:10 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57