Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

IsPostBack Not preserving data

Status
Not open for further replies.

Kalisto

Programmer
Feb 18, 2003
997
0
0
GB
Hopefully this is really obvious, but forgive me. I havent used .net for a while, and this isnt my home dev machine, so I cant look at old code.

I have a simple form which I am using to test various ideas out on at present.

in the pageLoad I have a load of variables being set

Code:
protected int testInt;

private void Page_Load(object sender, System.EventArgs e)
{
	if (!IsPostBack)
	{
		myArr = new ArrayList(10);
		testInt = new int();
		testInt = 5;
		//Set the Array Name
		myArr.Initialise();
	}
}

But when these are used late on in the program, in response to button presses and so on, the PostBack occurs, steps over the initialisation code, but all my arrays are undefined and my int has reset to 0

So am I doing something wrong here in my assumption that the variables should stay set? Or do I need to come up with some method of caching these variables on the server (In which case I either set them every pass, save in a DB, or persist them I guess)

K
 
All you need is an else block, but first let me explain

Any web page can be requested either using HTTP:GET or HTTP:pOST, the former is the initial request of the page the latter contain state info to be posted back to the page for further processing etc (this is to make things simple but it is very informal). The condition in which you set your variables is if (!PostBack) this means (If and only if this is the initial request for the page)

So “late in the program” all subsequent requests are indeed Postback = true
Which makes your condition (!PostBack) equal to false.

if (!IsPostBack)
{
myArr = new ArrayList(10);
testInt = new int();
testInt = 5;
//Set the Array Name
myArr.Initialise();
}
else
{
// Do whatever needs to be done “late in the program” in this block
}

Obviously, if you will set the array to the same exact values and dimension etc, you don’t need to define the array under condition to begin with. Define outside the if block.

Good luck


Walid Magd
 
Or do I need to come up with some method of caching these variables on the server

I'm not much of an ASP.Net or web guy, but I believe what you are referring to are session variables?
 
Walid, I only want to declare the array the first time the page is accessed which is why it is in the !IsPostBack block. I guess that when the page is recalled the variables are no longer set to values as they have gone out of scope, so I need to store my array in some kind of control which is able to persist the data between roundtrips. So I need some kind of server side persistence for each session.

K
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top