1

I'm with a problem and want to know if someone can help me.

I'm creating a table with some controls and I want to save all control values in every postback. As the controls are just defined after the page_load, I can't solve my problem this way:

object o;
protected void Page_Load(object sender, EventArgs e)
{
 o = createObject();
    Create_Table();
 if (Page.IsPostBack)
    Save_Data();
}

I thought I could execute Save_Data() at the begining of every postback event, but I think that should exist a better way to solve my problem.

Thanks.

userk
  • 169
  • 2
  • 3
  • 9

2 Answers2

1

Since you want it to be at the page level why not use ViewState? Since o appears to always be set with the same data there probably isn't need to set it more then once, though if you really want to you can remove the if not postback stuff...

protected object o
{
    get {
        return ViewState["o"];
    }
    set {
        ViewState["o"] = value;
    }
}

protected void Page_Load(object sender, EventArgs e)
{
    if (!Page.IsPostBack) { o = createObject(); }        
    Create_Table();
    if (Page.IsPostBack)
        Save_Data();
}
Peter
  • 9,643
  • 6
  • 61
  • 108
0

Your variable 'o' will not contain your original value once the postback is done. This is because each request creates a new page object on the server and your member variable values will be lost. It would be better to use the built in 'Session' property to save your data between requests.

See my answer here

Community
  • 1
  • 1
Mike Marshall
  • 7,788
  • 4
  • 39
  • 63
  • The thing is: I don't want the object as a session variable, I prefer to create it on every page_load. The object is created based on some files and its mains functions are: give the structure of the table and store tables values. The table will always have the same structure, but its values may vary, and I want to save them in the files the new values before anything happens. – userk Jun 16 '10 at 21:47