TheChaseMan's Frenetic SoapBox

Always looking for better ways to do things...

Static variables - cache storage

Static variables in a Web Form are stored in the Cache, so they will persist on a postback. Even though you can, it's probably not the best idea. Just remember that the Cache is not session-scoped, it is available across all sessions.

 


protected System.Web.UI.WebControls.DataGrid DataGrid1;
protected System.Web.UI.WebControls.LinkButton LinkButton1;
private static DataSet _ds = null;

private 
void LinkButton1_Click(object sender, System.EventArgs e)
{
   
//just here to cause a postback
}

private
void Page_Load(object sender, System.EventArgs e)
{
    if(_ds == null)
        SetDataSet();
    DataGrid1.DataSource = _ds.Tables[0].DefaultView;
    DataGrid1.DataBind();
}


private
void SetDataSet()
{
    _ds =
new DataSet();
    _ds.Tables.Add();
    _ds.Tables[0].Columns.Add();
    _ds.Tables[0].Rows.Add(
new object[] {"Hello"});
    _ds.Tables[0].Rows.Add(
new object[] {"World!"});
}

 


Digg!

posted on Wednesday, March 03, 2004 9:16 AM

Feedback

No comments posted yet.