Annoyance DropDownList: the same value will not trigger an event

I populated a dropdownlist control with different text properties, but each text property had ONLY a value (the text property was A, the value property was blah, the text property was B, the value property was blahblah, etc.)

ASP.net only checks the properties of the value on the back side and because ALL the values ​​were the same (for the reason of testing), this slight annoying behavior occurred. Is there any work? Does this mean that you can never have the same meaning?

+4
source share
6 answers

It looks like you are working on the wrong event. Try SelectedIndexChanged .

Verify that the AutoPostBack property AutoPostBack set to True .

Decided

OK, so I began to delve into this, as I was curious :)

There is a problem when binding data to non-standard values.

So, firstly, I publicly apologize for something else.

Repeat:

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 by changing the values ​​in the DropDownList. Please note that PostBack does not occur.

When I looked at Source, I realized that we needed to explicitly set the " value " attribute for the <option> elements generated by the server control, which made me 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 explicitly sets the values ​​to "1", "2", "3", etc., making them unique, while preserving the correct data in the list.

Obviously, you can change this to work with single-column lists, but just do a for loop and use the value i or something like that.

As for the good workarounds with DataSets, not sure.

Actually, will we present a list of parameters with the same values ​​for the user?

I personally do not think that, probably, why this "problem" was not solved :)

Enjoy it!

PS:

Oh, I also have to add if you want to use the text value in "fix" and then change it to SelectedItem and not to SelectedValue .

+8
source

ASP.NET cannot distinguish between different elements with the same values ​​in the drop-down list, because when the browser sends an HTTP POST, it sends only the selected value.

ASP.NET will find the FIRST item in the drop-down list with a value that matches.

You need to make sure that each item in the drop-down list has a different value. You can do this by adding a key to each value. In other words, instead of having a blah for each value, you should use blah-1, blah-2, etc.

+6
source

The problem is that if the selected index does not change, then postback does not work. In the case when the user makes the same choice, the selected index does not change.

Sorry this does not answer the question, but it explains the behavior as far as I know.

0
source

Highlighting SelectedIndexChanged does not even start because the entire listitem value in the dropdownlist element is the same. I have done some searches. This seems to be a common problem. I have not found a job yet.

0
source

You can use these values:

12

2: 2

3: 2

Where the second number is the "real" value. Then your event should fire, and you can analyze the "real" value in your code.

Why do you have a drop-down list where all values ​​are the same? Or just some of them are the same?

0
source

If you go back to the previous days of ASP.Net, the only thing that submits with submitting the form with <SELECT> is VALUE from <OPTION> . ASP.Net then works efficiently which item is selected by looking at that value in the list of data items.

You will also notice that if you have two elements with the same value, but with different labels, which, if you call the postback, the next time the form loads, the first will be displayed, even if you have the second, the one selected before you performed the reverse gear.


If you take a step back and look at the source data source - how would you determine which text value was selected if all you have is that value? How do you select this value from a database or from a list? How would you update this row in the database? If you try, you will find that .Net throws an exception because it cannot uniquely identify the string.

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

0
source

All Articles