Joel Holder

Math, coding, philosophy, art, and other things that interest me

View on GitHub
12 November 2008

Use the ASP.NET ViewState with WebUserControls’ public Properties

by Joel Holder

 
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..</div>

tags: