ASP.Net ViewState doesn't work when Control become Enable=False

China☆狼群 提交于 2019-12-25 02:53:53

问题


Why when a control does contain value but it's set to .Enable=False that all controls become disable (that's ok) but why that the ViewState doesn't retain the data on the next post back? If I get the UserControl without modifing its Enable state, the ViewState work between post back.

How can we disable a UserControl that all its control become disable (this part work) but all of them KEEP use the ViewState (this doesn't work)?

Clarification:

1)In the aspx.cs click button EDIT:

myControl.Enabled = false;
//This produce to have all controls in myControl to be disabled.

2)In the asp.cs: click Save to leave the EDIT state:

myControl.Enabled = true;
//This produce that all controls inside myControl are blank : no viewstate!

Other postback works because we do not put myControl.Enable to false.


回答1:


Specifying .Enable=False is like a server-side disable, NOT a client-side disable. This is for security purposes so you can truly disable an input element even if the user uses some client-side trickery to "re-enable" it. If you want to perform a client-side disabling, you'll need to use script for that, or perhaps use a readonly attribute.




回答2:


Have you tried Page.Form.SubmitDisabledControls = true;




回答3:


This is by design, if you want the control to use ViewState and still be disabled, then you have disable the control on the ClientSide. I use the Page.ClientScript to handle this and register your javascript to disable the control.



来源:https://stackoverflow.com/questions/2914527/asp-net-viewstate-doesnt-work-when-control-become-enable-false

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