No event fired when first item in dropdown select

H

hypomite

I have an handler for the SelectedIndexChanged event of a dropdown box. I
have also set the AutoPostBack option to True. When you select any item
besides the first one, the event sucessfully fires. However, if you ever
(even after you have selected other options previously) select the first
option in the dropdown, the AutoPostBack fires(the page reloads), but the
event does not. This is the only element on my page and ViewState is enabled
for both the page and the dropdown control.

Whats the deal here?
 
K

Ken Cox [Microsoft MVP]

Yup, this is a pain. Since you haven't changed the selection, the index hasn't
changed. Some people add a "---Select---" field to force users to make a move.

Ken

--
Microsoft MVPs have a question for *you*: Are you patched against the Worm?
http://www.microsoft.com/security/security_bulletins/ms03-026.asp



I have an handler for the SelectedIndexChanged event of a dropdown box. I
have also set the AutoPostBack option to True. When you select any item
besides the first one, the event sucessfully fires. However, if you ever
(even after you have selected other options previously) select the first
option in the dropdown, the AutoPostBack fires(the page reloads), but the
event does not. This is the only element on my page and ViewState is enabled
for both the page and the dropdown control.

Whats the deal here?
 
S

S. Justin Gengo

Here's a great solution to this problem:

Use the required field validator.

First give your drop down list a first value like: "Please Select"

DropDownList1.Items.Insert(0, "Please Select") '---If you need to add it
after a databind.

Set the required field validator to validate the drop down.

Set the required field validator's InitialValue property to the initial
text: "Please Select"

The only thing is if the drop down is set to autopost back then the
validator won't fire on the client side so in your selected index changed
handler use:

Page.Validate()

If Page.IsValid Then

'---Code to run when drop down list is changed.

End If



Sincerely,


--
S. Justin Gengo, MCP
Web Developer


Free code library at:
www.aboutfortunate.com

"Out of chaos comes order."
Nietzche
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top