开发者

ASP.NET validation issue - DropDownList being populated dynamically on client

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 easie开发者_Go百科st way around this? (I need to leave server validation turned on)


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜