Dropdown lists with the same value, different text

by Jesse 1. December 2008 11:00

I've been working on some calculators that are rather heavy on the number side, but because of the nature of what these things are, there's some issues that totally surprised me.  If you have two values in a dropdown and do a postback, unexpected things happen.

On a page, paste this into the source...

<asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="true" >
    <asp:ListItem Value="3">ItemA</asp:ListItem>
    <asp:ListItem Value="4">ItemB</asp:ListItem>
    <asp:ListItem Value="3">ItemC</asp:ListItem>
    <asp:ListItem Value="4">ItemD</asp:ListItem>
    <asp:ListItem Value="9">ItemE</asp:ListItem>
</asp:DropDownList>

Load it up and watch what happens (select ItemA, then ItemB, ItemC, ItemD, ItemE) and note the behavior.  This is because (as I found out) .net keeps track of it in viewstate by value, not index, text or anything else (as demonstrated).

"Why is this a problem?  When would you have two values the same, that'll never happen!"  - wrong!  In front of me is an xml doc with pressure values on it for the project I'm working on -- guess what, the pressure for itemC is the same as itemW, but the names are very different so this is a real world, totally practical reason why this should not work this way.

So how do you fix it?  Great question.  If I ever come up with a good one, I'll post up an answer ...unless someone else does?  I have a feeling it will be saving the index, the value and the text into viewstate or a combination thereof by using an inherited control.  Just a thought.

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Tags:

Comments

Powered by BlogEngine.NET 1.4.5.0
Theme by Mads Kristensen

About the author

Like the description says, at my core, I'm a scientist and engineer.  I came from humble beginnings on a 486DX2 Packard Hell playing doom2 on IPX to in a small time retail shop and got into hardware (ISO layers FTW!) and it was all downhill from there.  I'm infinitely curious about almost everything and always wanting to know.

According to personality tests (real ones) I classify under "Rational" more specifically, a Fieldmarshal.  I think there's something to that.

Some of the stuff I'm currently into/researching...

Sitefinity

Ninject

Subsonic 

Currently working on ...
i did the hundred


and some extra stuff

Disclaimer

The opinions expressed herein are my own personal opinions and do not represent my employer's, their brother, their dog, cat, ferret nor gold fish's view in anyway.  At all.  Ever.

© Copyright 2007-2009

Month List