Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

User control (ascx) and properties

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.ToString(ViewState["Title"]); }
        set { ViewState["Title"] = value; }
    }

I can't say I'm real impressed with this though, as the more properties a user control has the more crap you'll be sticking in the ViewState. Is there a better way to persist properties?

like image 357
Jagd Avatar asked Jul 24 '09 19:07

Jagd


People also ask

What is Property in user control?

User Control properties are used to set the values of a User Control from the parent page.

What are user controls and custom controls?

The main difference between them- User Control is a page file with extension . ascx which can only be used within a single application or project But custom controls are assemblies(dll files) that can be used in multiple applications.

What are user controls?

User controls are containers into which you can put markup and Web server controls. You can then treat the user control as a unit and define properties and methods for it. Custom controls. A custom control is a class that you write that derives from Control or WebControl.


3 Answers

It depends. If you need to property values to be persisted beyond a post-back then you'll either have to use ViewState or Session. Since those controls are re-created on each post back you can't really maintain that state otherwise.

like image 143
Sayed Ibrahim Hashimi Avatar answered Oct 07 '22 13:10

Sayed Ibrahim Hashimi


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 :-)

like image 23
dariom Avatar answered Oct 07 '22 13:10

dariom


Your problem is exactly what ViewState is for: To persist properties of a control across postbacks, so your solution is just fine.

You could save it in session, but that really just puts the burden on the server. Depending on the number of users you have, this could get really ugly really quickly.

Also keep in mind that you have to do some housekeeping if you use session. For example, if you want to use your user control twice on the same page, you need to make sure that each control uses unique session variables.

like image 21
Stefan Avatar answered Oct 07 '22 12:10

Stefan