views:

126

answers:

1

I'm populating a DropDownList using JS on the client and validating with a RequiredFieldValidator.

This works fine on the client but the Page.IsValid consistently comes back false on the server.

Is this because the selected value wasn't in the DropDownList when it was first served to the page?

What's the easiest way around this? (I need to leave server validation turned on)

+2  A: 

Is this because the selected value wasn't in the DropDownList when it was first served to the page?

Yes. You'll probably notice that your dropdownlist will contain no items when you do your postback, and yes, this is because you're adding your items on the client side. Any items that you add to a control on the client are totally unknown to the server. Therefore, your server validation will always fail, since that field is required.

In fact, adding items dynamically with client script will trigger EventValidation to complain that there is a possible security problem, and you'll have had to set EnableEventValidation to false in your <%@ Page %> directive to be able to post.

The best way around this is to either

  1. Generate your items on the server side, or

  2. Not use a server control for this (use a regular non-asp.net select list) and manually validate it on the server by looking at the posted values.

womp
Fantastic, thanks very much.
Colin Jones