A instance of a class is created in the partial class of an aspx page.Under page_load or button click method I’m trying to set the value to the class. but when each postback takes place new instance is created and I’m losing the previous value.
public partial class DatabaseSelection : System.Web.UI.Page
{
DBProperties dbpro;
Metadata obmeta;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
dbpro = new DBProperties();
}
If you need this instance by application throw it in the Application or use a Singleton collection:
Application[“Foo”] = new MyClass();
See other answer.
If you need this for a single request (which seems unlikely here):
HttpContext.Current.Items[“Foo”] = new MyClass();
If you need this across requests the the following are all options depending on your scenario:
I am not really sure I would recommend the Singleton pattern. Technically singletons will stick around as long as your AppDomain effectively being similar to the Application variable.