User control (ascx) and properties

前端 未结 6 2220
终归单人心
终归单人心 2020-12-29 04:53

The only way I\'ve found to persist property values within a user control is to use the ViewState.

public string Title {
        get { return Convert.ToStrin         


        
6条回答
  •  忘掉有多难
    2020-12-29 05:51

    There's no problem at all with using ViewState to store property values for a user control.

    Your statement "the more properties a user control has the more crap you'll be sticking in the ViewState" isn't necessarily true though. It's certainly possible to have ViewState track values of properties for controls but not store data in the __VIEWSTATE hidden form field variable.

    Sounds crazy right? See TRULY Understanding ViewState for a brilliant article about how ViewState works.

    It depends on when you initialize the properties of your controls in it's lifecycle. ViewState will only be stored in the hidden __VIEWSTATE field after the StateBag for a control starts tracking changes to property values. This happens in the OnInit method for a control which is early in the lifecycle, but there are techniques to set your property values earlier that won't incur the cost of __VIEWSTATE bloat and will still give you all the benefits.

    See the linked article. It discusses everything very clearly and better than I can :-)

提交回复
热议问题