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 comment