This is a migrated thread and some comments may be shown as answers.

Automatic Methods don't hold their values after postback

1 Answer 62 Views
Ajax
This is a migrated thread and some comments may be shown as answers.
Sintayehu
Top achievements
Rank 1
Sintayehu asked on 04 Mar 2009, 04:06 PM
//page load

 

protected void Page_Load(object sender, EventArgs e)

 

{

// check if StoreIt is not empty
if(setStoreIt.Equals("KeepItAlive")){
//Do something
}
}


public string storeIt{get; set}

private void setStoreIt()
{
    StoreIt = "KeepItAlive";
}

Unfortunately the setStoreIt Automatic Method gets reset everytime a postBack happenes.

I always used HiddenFields to get around this issue.

But today I am presented with an issue where i have to keep a <List> alive.

Any ideas?

Is this a problem with ajaxified pages?

Can I add the automatic method in the aJaxManajer? Ha Ha

Thanx!

1 Answer, 1 is accepted

Sort by
0
Accepted
Mark Fitzpatrick
Top achievements
Rank 1
answered on 04 Mar 2009, 04:50 PM
They won't because you're not storing them anywhere. Once the page is done processing the local variables are destroyed. You have to store the data somewhere in order to retrieve it across postbacks. ASP.Net does provide a mechanism around this through the ViewState. Let's take a generic list of integer values

public List<int> myList
{
    get
        {
            if(myList != null)
                return (List<int>)ViewState["myList"];
            else
                return null;
                // you could also set it to return a new List<int> but I prefer nulls.
        }
        set
        { 
            ViewState["myList"] = value;
        }
}

You can store your own objects there as well so long as they are marked Serializeable on the class.
Tags
Ajax
Asked by
Sintayehu
Top achievements
Rank 1
Answers by
Mark Fitzpatrick
Top achievements
Rank 1
Share this question
or