ASP.NET viewstate support for custom web server controls
I am not able to understand the following : (It is related to adding viewstate support when creating custom web server controls)
"However, 开发者_运维技巧if these properties need to be manipulated inside code, you might want to provide ViewState support to ensure a consistent developer experience similar to that of other ASP.NET controls. This will ensure that the properties set inside server code are not suddenly switched back to markup values after a postback."
Can someone please elaborate ?
Thanks.
ViewState is used to maintain page state between postbacks. If your controls don't persist their state as the page goes through its lifecycle, the next time the page loads they will revert to whatever values were originally provided by the developer in the ASP markup.
It means that the values won't survive a postback without using viewstate (or taking other precautions). So if you're designing controls for others to use, you should take steps to make the controls behave like standard MS provided controls.
When you build a custom control, you are responsible for adding the child controls using createchildcontrols and rendering them. (or use default rendering order).
The view state is automatically maintained for child controls where applicable (say texbox, label etc) unless you dynamically change the order of controls being added. The page keeps track of the order of controls on it when posting back and viewstate values are populated based on this order. As long as you don't change this order during postback, viewstate will be correctly maintained. As far as I know, there is no "extra support" you need to do to maintain viewstate for child controls.
Loading Postback values have nothing to do with viewstate. If the user changed a value of a textbox while posting the page back to the server, the viewstate value will be overriden with the posted back value.
If you can specify the context where you read it or provide a link, I can elaborate more.
精彩评论