Use the ASP.NET ViewState with WebUserControls’ public Properties

 
This technique works with WebForms, but becomes much more useful when you site WebUserControls on a WebForm and need to set/get values between the parent Form and a child UserControl.
 
Put something like this in your WebUserCotrol1.
 
public int Total {

        get { return ViewState["intTotal"] != null ? Int32.Parse(ViewState["intTotal"].ToString()) : 0; }

        set { ViewState["intTotal"] = value; }

}

Site the UserControl on WebForm1.

Then you can access the Property in the UserControl from WebForm1’s code behind like this.

int newTotal = this.WebUserControl1.Total;

Simple..  ViewState will hold the value between postbacks..

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

w

Connecting to %s