Probably a simple answer to 'state'

Discussion in 'ASP .Net' started by Ralph Krausse, Sep 13, 2004.

  1. 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
     
    Ralph Krausse, Sep 13, 2004
    #1
    1. Advertising

  2. 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

    "Ralph Krausse" <> wrote in message
    news:...
    > 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
     
    Kevin Spencer, Sep 13, 2004
    #2
    1. Advertising

  3. Ralph Krausse

    Joerg Jooss Guest

    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
     
    Joerg Jooss, Sep 13, 2004
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Rob Meade
    Replies:
    3
    Views:
    2,346
    Cowboy \(Gregory A. Beamer\)
    Nov 25, 2003
  2. Daniel Frey

    Simple Question - Simple Answer?

    Daniel Frey, Dec 28, 2004, in forum: XML
    Replies:
    4
    Views:
    845
    Daniel Frey
    Jan 12, 2005
  3. Greg Corradini

    Simple mx.ODBC prob seeks simple answer

    Greg Corradini, Apr 6, 2007, in forum: Python
    Replies:
    0
    Views:
    317
    Greg Corradini
    Apr 6, 2007
  4. Steve Holden
    Replies:
    1
    Views:
    406
    Dennis Lee Bieber
    Apr 6, 2007
  5. Eric
    Replies:
    4
    Views:
    204
    Ray at
    Sep 24, 2003
Loading...

Share This Page