1402 posts
  • Author had a File in a Mini Bundle
  • Has been a member for 4-5 years
  • Sold between 1 000 and 5 000 dollars
  • Exclusive Author
  • Bought between 50 and 99 items
  • Referred between 10 and 49 users
  • United States
WebSmacker says

I am setting the value in a dropdownlist using javascript, but it doesn’t fire the selectedindexchanged event in C#. Any ideas?

614 posts
  • Envato Studio (Microlancer) Beta Tester
  • Exclusive Author
  • Has been a member for 2-3 years
  • Referred between 1 and 9 users
  • Sold between 10 000 and 50 000 dollars
Typps says

Just tested and it seems to work fine here. You might want to double check how you are setting the value in javascript.

If you are creating the items for your dropdownlist imperatively as opposed to declaratively, then make sure you are adding the items early (i.e. before the page load event handler ). The dropdownlist will also maintain it’s items in viewstate, so if you populated it once imperatively, then there is no need to repopulate after a postback, if it’s not necessary for your solution that is.

Repopulating late in the pages life cycle can cause you problems as well. It’s not easy to tell you what went wrong without looking at how you are setting the value in javascript, how you are populating the dropdownlist with items and lastly, how you have declared/wired the SelectedIndexChanged event handler.

1402 posts
  • Author had a File in a Mini Bundle
  • Has been a member for 4-5 years
  • Sold between 1 000 and 5 000 dollars
  • Exclusive Author
  • Bought between 50 and 99 items
  • Referred between 10 and 49 users
  • United States
WebSmacker says

Thanks. With your suggestion I was able to pinpoint the issue. I was adding an item to the dropdownlist in javascript and it didn’t like that I was doing that.

320 posts
  • Author had a File in an Envato Bundle
  • Bought between 1 and 9 items
  • Exclusive Author
  • Has been a member for 5-6 years
  • Sold between 1 000 and 5 000 dollars
  • United States
jwmcpeak says

Mixing WebForms controls with JavaScript/Ajax can be troublesome. If you want to use the latter, I suggest not using controls.

614 posts
  • Envato Studio (Microlancer) Beta Tester
  • Exclusive Author
  • Has been a member for 2-3 years
  • Referred between 1 and 9 users
  • Sold between 10 000 and 50 000 dollars
Typps says

Thanks. With your suggestion I was able to pinpoint the issue. I was adding an item to the dropdownlist in javascript and it didn’t like that I was doing that.

Ahh your adding an item from the clientside. I was under the impression you were setting an existing item in your dropdownlist as selected from the client and then expecting the SelectedIndexChanged event handler to fire, which is also what I tested ^^

The SelectedIndexChanged handler fires only when an option element ( ListItem) selected attribute/property changes and not when a new option element or ListItem is added, whether clientside or on the server.

There is no pretty builtin way to fix your problem. The best advice is the one from jwmcpeak because your adding new options to the dropdownlist control from the client, so you are changing existing options in a dropdownlist that were rendered from the server. ASP .NET webforms has a builtin EventValidation mechanism where it checks exactly this and treats it as a potential threat. Ofcourse you can disable EventValidation but not recommended as you open up a security hole.

If the new option values you are adding are known values, you can register them for event validation and workaround but it’s still long winded and ends up being a convoluted solution.

5390 posts
  • Australia
  • Bought between 100 and 499 items
  • Envato Studio (Microlancer) Beta Tester
  • Exclusive Author
  • Has been a member for 4-5 years
  • Interviewed on the Envato Notes blog
  • Referred between 1 and 9 users
  • Sold between 1 000 and 5 000 dollars
Australia says

Just like to say ale55andro , great answers dude !

614 posts
  • Envato Studio (Microlancer) Beta Tester
  • Exclusive Author
  • Has been a member for 2-3 years
  • Referred between 1 and 9 users
  • Sold between 10 000 and 50 000 dollars
Typps says

Just like to say ale55andro , great answers dude !

hey, thanks :P

by
by
by
by
by
by