1449 posts
  • Has referred 10+ members
  • Has sold $1,000+ on Envato Market
  • Has collected 50+ items on Envato Market
  • Had an item featured in an Envato Pack
+3 more
WebSmacker says

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

660 posts
  • Has been part of the Envato Community for over 2 years
  • Has referred 1+ members
  • Has sold $10,000+ on Envato Market
  • Has been a beta tester for an Envato feature
+1 more
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.

1449 posts
  • Has referred 10+ members
  • Has sold $1,000+ on Envato Market
  • Has collected 50+ items on Envato Market
  • Had an item featured in an Envato Pack
+3 more
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
  • Has been part of the Envato Community for over 5 years
  • Has sold $1,000+ on Envato Market
  • Had an item featured in an Envato Bundle
  • Has collected 1+ items on Envato Market
+2 more
jwmcpeak says

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

660 posts
  • Has been part of the Envato Community for over 2 years
  • Has referred 1+ members
  • Has sold $10,000+ on Envato Market
  • Has been a beta tester for an Envato feature
+1 more
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.

5492 posts
  • Has been part of the Envato Community for over 4 years
  • Has referred 1+ members
  • Located in Australia
  • Has sold $1,000+ on Envato Market
+4 more
Australia says

Just like to say ale55andro , great answers dude !

660 posts
  • Has been part of the Envato Community for over 2 years
  • Has referred 1+ members
  • Has sold $10,000+ on Envato Market
  • Has been a beta tester for an Envato feature
+1 more
Typps says

Just like to say ale55andro , great answers dude !

hey, thanks :P

by
by
by
by
by
by