DropDownList forgets SelectedIndex

F

Fred Sawtelle

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
 
A

Alex Homer

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...
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top