Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > ASP .Net Web Controls > DropDownList forgets SelectedIndex

Thread Tools

DropDownList forgets SelectedIndex

Fred Sawtelle
Posts: n/a
I have a DropDownList which is databound on first page
load to a dataview, and which has its AutoPostback set to
true (yes, this is necessary). For each item in the
list, the text is either a department name or an alias
for a department, and the value is a DeptID. Departments
and their respective aliases have the same DeptID in the
value field.

My DropDownList appears to be saving its SelectedValue in
the ViewState for server trips. I thought it would be
SelectedIndex. But I have found that when an item is
selected which shares its value with other items, the
first item alphabetically by text field is the one
selected after a server trip.

For example, an item may have a value of 20 and a text
of "Regular Department Name", and another item may have a
value of 20 and a text of "Alias Department Name". If I
select "Regular Department Name" in the list, it performs
its AutoPostback, and when the page loads it sets itself
to "Alias Department Name". However, if I
preface "Regular" with "AAA" (making it first
alphabetically), it doesn't do this. If I
preface "Alias" with "RRR" (making it second
alphabetically), it doesn't do it.

I've seen this behavior with several different examples.
The simple explanation would be that it's just looking
for the first occurrence of its SelectedValue and setting
SelectedIndex to that.

I need it to remember the actual SelectedIndex. I should
mention that this control is part of a server control
that is rendered in a portal, and its name has been
randomly changed by the time it makes it to HTML at the
client, so getting the SelectedIndex from Request.Form is
not an option.

Any help with this would be appreciated.

Fred Sawtelle
Reply With Quote
Alex Homer
Posts: n/a
ASP.NET can only tell which item is selected by looking at the contents of
the Request collections (Request.Form usually). The viewstate only tells it
what was selected when the page was created and sent to the browser (this is
how it knows if a value has changed and so it can fire the appropriate
OnXxxxChanged event). HTML rules and browser behavior result in a <select>
element posting back the currently selected value if there is a value
attribute for that <option>, or the text of the <option> element if there is
no value attribute. So you need to ensure that the value of each item in the
list is unique or you'll get the wrong answer...

Reply With Quote

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
When I set the DropDownList.SelectedIndex of ONE DropDownList they ALL change! S_K ASP .Net 5 11-14-2007 04:51 PM
DropDownList inside GridView: How to set SelectedIndex for the DropDownList? keithb ASP .Net 1 11-01-2006 05:24 AM
How do I move a dropdownlist to a selectedindex value? jm ASP .Net 1 02-11-2004 05:44 PM
DropDownList SelectedIndex value is not working for the last item. Bryan Poteet ASP .Net 0 12-04-2003 06:06 PM
DropDownList SelectedIndex stuck at -1 after IsPostBack Matthew ASP .Net 1 11-12-2003 08:12 PM