Probably a simple answer to 'state'

R

Ralph Krausse

My source to my test is below. I am trying to figure out state. I
create a int and a Test object and set them to some value on
Page_Load. When I click my button on my web page, Button1_Click gets
called but a = 0 and objTest.Name is null. Now it does make sense than
when the button is clicked, that a should be 0 and objTest should be
null but I thought that ASP.NET took care of this. Do I have to go
back to old ASP days and set this stuff to Application("objTest") =
objTest???? There must be a better way. Please enlighten me....

____________________________________________________________________________

My Test Class...

public class Test
{
public Test()
{
//
// TODO: Add constructor logic here
//
}

private string strName;
public string Name
{
get { return strName; }
set { strName = value; }
}

My ASP.NET PAGE

public class WebForm1 : System.Web.UI.Page
{
protected System.Web.UI.WebControls.Button Button1;
public Test objTest;
public int a;
private void Page_Load(object sender, System.EventArgs e)
{
if(!IsPostBack)
{
objTest = new Test();
objTest.Name = "test";
a = 10;
}
}

private void Button1_Click(object sender, System.EventArgs e)
{
a = 199;
objTest.Name = "a";
}
}



Thanks
Ralph Krausse

www.consiliumsoft.com
Use the START button? Then you need CSFastRunII...
A new kind of application launcher integrated in the taskbar!
ScreenShot - http://www.consiliumsoft.com/ScreenShot.jpg
 
K

Kevin Spencer

Hi Ralph,

An ASP.Net Page is really no different "under the covers" than an ASP page.
That is, it exists and works in the context of an HTTP request, which is
stateless. Just as an ASP page, the entire Page class has to be
reconstructed with each request, or PostBack. Now, also note that the Page
class has a number of built-in Event Handlers, which you can override to add
your own code to the Page execution. These Event Handlers fire in a certain
sequence (se
http://msdn.microsoft.com/library/d...guide/html/cpconControlExecutionLifecycle.asp
for details). The Page_Load Event Handler fires BEFORE the Events are
handled. Hard to handle an Event if the class it refers to doesn't exist
yet, eh?

Following me so far? Okay, so in your Page_Load Event Handler, you create an
instance of your "Test" class. You do NOT create an instance of it if the
Page is Posted Back. Now, your Event Handler for the Button click tries to
assign a value to its members. However, as it doesn't exist, it throws an
exception because the Test class instance doesn't exist. Where you're seeing
"a" as being 0, I don't know. I can only guess that you're checking before
the value is assigned in the Button click Event handler.

--
HTH,
Kevin Spencer
..Net Developer
Microsoft MVP
I get paid good money to
solve puzzles for a living
 
J

Joerg Jooss

Ralph said:
My source to my test is below. I am trying to figure out state. I
create a int and a Test object and set them to some value on
Page_Load. When I click my button on my web page, Button1_Click gets
called but a = 0 and objTest.Name is null. Now it does make sense than
when the button is clicked, that a should be 0 and objTest should be
null but I thought that ASP.NET took care of this. Do I have to go
back to old ASP days and set this stuff to Application("objTest") =
objTest???? There must be a better way. Please enlighten me....

____________________________________________________________________________

My Test Class...

public class Test
{
public Test()
{
//
// TODO: Add constructor logic here
//
}

private string strName;
public string Name
{
get { return strName; }
set { strName = value; }
}

My ASP.NET PAGE

public class WebForm1 : System.Web.UI.Page
{
protected System.Web.UI.WebControls.Button Button1;
public Test objTest;
public int a;
private void Page_Load(object sender, System.EventArgs e)
{
if(!IsPostBack)
{
objTest = new Test();
objTest.Name = "test";
a = 10;
}
}

private void Button1_Click(object sender, System.EventArgs e)
{
a = 199;
objTest.Name = "a";
}
}

Each request is serviced by a new Page instance. If Test is supposed to
maintain state across postbacks, you have to store it somewhere else, e.g.
SessionState or ViewState.

Cheers,
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top