4

i've populated a dropdownlist control with different text properties but each text properties had THE SAME value (text property was A, value properties is blah,text property was B, value properties is blahblah, etc... )

ASP.net only checks value properties on postback and because ALL values were the same (for testing reason) this little annoying behavior happened. Is there a work around? does this mean you can't never have the value to be the same?

Jack
  • 9,843
  • 23
  • 78
  • 111

6 Answers6

8

Sounds like you are working on the wrong event. Try SelectedIndexChanged.

Ensure you also have the AutoPostBack property set to True.

Resolved

OK, so I got digging on this since I was curious :)

There is a "problem" when databinding with non-unique values.

So, firstly, I publicly apologise for saying otherwise.

To replicate:

ASPX

    <asp:DropDownList ID="myDDL" runat="server" AutoPostBack="True">
    </asp:DropDownList>
    <asp:Label ID="lblSelItem" runat="server"Text="Currently Selected Item: 0"></asp:Label>
    <asp:Label ID="lblSelVal" runat="server" Text="Currently Selected Value: X"></asp:Label>

Code-Behind

    List<string> MyData()
    {
        List<string> rtn = new List<string>();
        rtn.Add("I am the same value!");
        rtn.Add("I am the same value!");
        rtn.Add("I am the same value!");
        rtn.Add("I am the same value!2");
        return rtn;
    }

    protected void Page_Init()
    {
        if (!Page.IsPostBack)
        {
            // Load the Data for the DDL.
            myDDL.DataSource = MyData();
            myDDL.DataBind();
        }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        // Display the Currently Selected Item/Value.
        lblSelItem.Text = "Currently Selected Item: " + myDDL.SelectedIndex.ToString();
        lblSelVal.Text = "Currently Selected Value: " + myDDL.SelectedValue;
    }

Run, changing the values in the DropDownList. Note that a PostBack does not occur.

When looking at the Source, I realised that we need to explicitly set the "value" attribute for the <option> elements generated by the server control, which lead me to do something like:

New Code-Behind

    Dictionary<string, string> MyTwoColData()
    {
        Dictionary<string, string> rtn = new Dictionary<string, string>();
        rtn.Add("1", "I am the same value!");
        rtn.Add("2", "I am the same value!");
        rtn.Add("3", "I am the same value!");
        return rtn;
    }

    protected void Page_Init()
    {
        if (!Page.IsPostBack)
        {
            // Load the Data for the DDL.
            Dictionary<string, string> data = MyTwoColData();

            foreach (KeyValuePair<string, string> pair in MyTwoColData())
            {
                myDDL.Items.Add(new ListItem(pair.Value, pair.Key));
            }

            myDDL.DataBind();
        }
    }

This explcitly sets the values to the "1", "2", "3" etc making them unique, while still displaying the correct data within the list.

Obviously, you can change this to work with single-column lists but just running through a for loop and using the value of i or something.

As to good workarounds with DataSets, not sure.

Realistically, would we present a list of options with the exact same values to the user?

I personally think not, which is probably why this "problem" hasn't been addressed :)

Enjoy!

PS:

Oh, I should also add, if you want to use the text value in the "fix" then change it to SelectedItem rather than SelectedValue.

Community
  • 1
  • 1
Rob Cooper
  • 28,567
  • 26
  • 103
  • 142
  • It won't even trigger the SelectedIndexChanged event. I have the autopostback set to true. – Jack Nov 25 '08 at 21:43
6

ASP.NET can't distinguish between different items with the same values in the dropdown because when the browser sends the HTTP POST, it sends just the selected value.

ASP.NET will find the FIRST item in the dropdown with a value that matches.

You need to ensure that each item in the dropdown has a distinct value. You could do this by adding a key to each value. In other words, instead of having "blah" for each value, you'd use "blah-1", "blah-2", etc.

Joseph Anderson
  • 2,838
  • 2
  • 26
  • 26
  • I have two identical drop downlists in two different web pages which read and get filled from the same data source and they both have identical values but different texts . This issue only occurs in one webpage!!! how come? – Niloofar May 18 '17 at 14:59
0

The problem is that if the selected index doesn't change the postback won't fire. In the case where the user makes the same selection, the selected index does not change.

Sorry that this doesn't answer the question, but it does explain the behavior as far as I know.

TGnat
  • 3,903
  • 8
  • 35
  • 46
  • This isn't entirely correct. The SelectedIndex WILL change if the user selects a DIFFERENT item (irrespective of what the value that is displayed). AutoPostback then ensures that a PostBack is performed when the item is changed, ensuring the ViewState reflects the currently selected item. – Rob Cooper Nov 25 '08 at 21:41
  • So much for reading the whole question! I didn't see the part where he said they all had the same text. My anwer assumes that the same item was selected. – TGnat Nov 25 '08 at 21:47
  • Turns out a PostBack doesn't occur if the value is the same, regardless of index! Lesson learned (although I would probably never display the same values!). – Rob Cooper Nov 25 '08 at 22:17
0

The SelectedIndexChanged won't even trigger because all the listitem value in the dropdownlist control are the same. I did some googling. It seem like this is the common problem. I haven't found any work around yet.

Jack
  • 9,843
  • 23
  • 78
  • 111
  • That's because there is none. Postback only works if values are different. The only way around this is to "force" the user to reset the DropDownList by placing a button or some other control next to the DropDownList, with its own ClickEvent() to reset the control and that will force a postback. – Fandango68 Jun 25 '15 at 03:58
  • Or use the OnClientClick() and some javascript to reset the control. Same thing – Fandango68 Jun 25 '15 at 03:58
0

You could use values like this:

1:2

2:2

3:2

Where the second number is the "real" value. Then your event should fire and you can parse out the "real" value in your code behind.

Why do you have a drop down where all of the values are the same? Or is just that some of them are the same?

JasonS
  • 23,480
  • 9
  • 41
  • 46
0

If you think back to pre ASP.Net days then the only thing that is send with a form submit from a <SELECT> is the VALUE of the <OPTION>. ASP.Net then effectively works out which item is selected by looking up this value in the list of data items.

You will also notice that if you have two items with the same value but different labels that if you do trigger a postback the next time the form loads the first one will be displayed, even if you have the second one selected before you performed the postback.


If you take a step back for a moment and consider your original data source - how would you identify which text value was selected if all you have is the Value? How would you select that value from a database, or from a list? How would you update that row in the database? If you try it you will find that .Net throw an Exception because it cannot uniquely identify the row.

Therefore you need to add a unique key to your data.

samjudson
  • 56,243
  • 7
  • 59
  • 69