Validators: Do they Stay disabled on postback when disabled ClientSide?

你说的曾经没有我的故事 提交于 2019-12-12 16:18:33

问题


I have been learning alot about the standard asp.net Validators, and my latest discovery was about how to disable a validator client side, which was pretty cool.

now, if my initial post has the validator enabled, but client side, i disable it, does the server side recognize the client side change, and keep it, or does it get re-enabled the the page is sent back to the user?

Thanks!

Nate


回答1:


.NET Server side validator controls will be reset to whatever they have been set to last in the server side code during a postback.

So for example if you have set a required field validator to rqvControl.enabled = true in its .aspx tag, then after a postback it will be enabled no matter what its state was client side.

If you are setting the state of a validator on the client side, and you want to persist it, then you will need to set a value that you can read in your server code during a postback. This can be as simple as setting a hidden field value from your javascript that is doing the enable/disable operation. In your codebehind just handle the enabled state of your validator based off the value in your hidden field.




回答2:


How do you disable them on the client side? I would imagine that unless the act of disabling them on the client does a postback that the viewstate will remain unchanged and hence they will be reenabled on the next page refresh (just theorizing here).

Try it yourself and if it doesn't work, you could perhaps add a hidden field which you also set on the client when you disable the validator and then check that field on the server side during a postback to know whether to enable/disable them there.



来源:https://stackoverflow.com/questions/448444/validators-do-they-stay-disabled-on-postback-when-disabled-clientside

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!