1554 posts
  • Has been part of the Envato Community for over 4 years
  • Had an item featured in an Envato Pack
  • Has sold $5,000+ on Envato Market
  • Located in United States
+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?

723 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.

1554 posts
  • Has been part of the Envato Community for over 4 years
  • Had an item featured in an Envato Pack
  • Has sold $5,000+ on Envato Market
  • Located in United States
+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.

723 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 !

723 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

Helpful Information

  • Please read our community guidelines. Self promotion and discussion of piracy is not allowed.
  • Open a support ticket if you would like specific help with your account, deposits or purchases.
  • Item Support by authors is optional and may vary. Please see the Support tab on each item page.

Most of all, enjoy your time here. Thank you for being a valued Envato community member.

Post Reply

Format your entry with some basic HTML. Read the Full Details, or here is a refresher:

<strong></strong> to make things bold
<em></em> to emphasize
<ul><li> or <ol><li> to make lists
<h3> or <h4> to make headings
<pre></pre> for code blocks
<code></code> for a few words of code
<a></a> for links
<img> to paste in an image (it'll need to be hosted somewhere else though)
<blockquote></blockquote> to quote somebody

:grin: :shocked: :cry: Complete List of Smiley Codes

by
by
by
by
by
by