Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > Probably a simple answer to 'state'

Reply
Thread Tools

Probably a simple answer to 'state'

 
 
Ralph Krausse
Guest
Posts: n/a
 
      09-13-2004
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
 
Reply With Quote
 
 
 
 
Kevin Spencer
Guest
Posts: n/a
 
      09-13-2004
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/de...nLifecycle.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

"Ralph Krausse" <> wrote in message
news: om...
> 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



 
Reply With Quote
 
 
 
 
Joerg Jooss
Guest
Posts: n/a
 
      09-13-2004
Ralph Krausse wrote:
> 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,

--
Joerg Jooss



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Wrong answer equals to a blank answer or not? Zadkin Microsoft Certification 8 06-27-2006 01:51 PM
Wrong answer equals to a blank answer or not? Zadkin Microsoft Certification 0 06-23-2006 09:17 PM
Detailed question, probably a simple answer - Windows XP Mike Computer Support 2 04-14-2005 05:49 AM
Mental Block - probably easy answer...validation summary Rob Meade ASP .Net 3 11-25-2003 03:42 PM
Form Field/ Form Submit Problems (probably an easy answer...) Eric ASP General 4 09-24-2003 12:34 PM



Advertisments