1

I have a scenario that my DropDownList is dynamically populated by the end user. But, sometimes, two or more items can have the same value. For example:

<asp:DropDownList runat="server" ID="ddl" AutoPostBack="true">
    <asp:ListItem Text="Item 1" Value="Item 1" />
    <asp:ListItem Text="Item 2" Value="Item 2" />
    <asp:ListItem Text="Item 3" Value="Item 2" />
</asp:DropDownList>

When I select the third item, ASP.NET selects the second one during the page refresh, because both have the same value. So, it does not use the selected index, but the selected value. Is there a way to change this behavior on my application?

I add all the controls dynamically, so it would be nice if I could have an elegant way of doing this automatically for each control that I add to the web page. Any suggestions?

Update 1:

Added a sample project to github: https://github.com/vmrocha/dropdown-issue

Vinicius Rocha
  • 3,602
  • 4
  • 26
  • 37
  • In you code example, the `Text` and `Value` properties are the same. Is it the case in your production code? I am pretty sure it's not but I just want to be sure... – ConnorsFan Apr 03 '16 at 19:00
  • No, that is not the case. You are correct. In the real application Text and Value are different. – Vinicius Rocha Apr 03 '16 at 19:03
  • Can you post the relevant code where you add the items (and say in which event handler you add them)? – ConnorsFan Apr 03 '16 at 19:12
  • Yes I can, but actually the problem is very easy to reproduce even if the DropDownList is static to the page. I will post a sample project on github and will be back to post the link here. – Vinicius Rocha Apr 03 '16 at 19:21
  • I can reproduce the problem. The reason I am asking is that I have an idea that works if implemented in markup but not (at least not easily) when the items are added in code. – ConnorsFan Apr 03 '16 at 19:24
  • 1
    I will add an answer with the first version of my suggestion (which works in markup). If the idea suits your needs, I will edit it afterwards with the solution for code-behind additions. I found the way to do it. – ConnorsFan Apr 03 '16 at 19:44
  • Please tell me if the idea of a custom attribute can help you. The version that works when adding the items in code-behind is much more complicated... – ConnorsFan Apr 03 '16 at 19:57
  • I will test to check if it works and let you now. – Vinicius Rocha Apr 03 '16 at 19:58
  • Yes, it works! I will mark it as the correct answer. Thank you. – Vinicius Rocha Apr 03 '16 at 20:35
  • You're welcome. In case you need to add items from code-behind, the custom tags can be preserved with the technique described here (gleapman's answer gives the full code): http://stackoverflow.com/questions/1313447/listitems-attributes-in-a-dropdownlist-are-lost-on-postback/5956997 – ConnorsFan Apr 03 '16 at 21:21

1 Answers1

2

You can add a custom attribute to each Item, store the actual value in it and retrieve it in code-behind. The Value attribute can then be set to different values for all items (ex. the index):

<asp:DropDownList runat="server" ID="ddl" AutoPostBack="true">
    <asp:ListItem Text="Item 1" Value="1" Tag="Value1" />
    <asp:ListItem Text="Item 2" Value="2" Tag="Value2" />
    <asp:ListItem Text="Item 3" Value="3" Tag="Value2"/>
</asp:DropDownList>

In code-behind:

string actualSelectedValue = ddl.SelectedItem.Attributes["Tag"];
ConnorsFan
  • 58,595
  • 11
  • 85
  • 120