Data / Values keep dissapearing!!!

Discussion in 'ASP .Net' started by =?Utf-8?B?VG9kZCBKYXNwZXJz?=, Apr 5, 2007.

  1. Hey guys,

    I guess I don't understand this whole Post-Back thing. I'm writing an
    ASP page using VS2005 in C#. On the page I'm working on, I set several
    variables on Form Load. I then have a button which dynamically builts some
    objects. I take a COUNT of these objects and set it to a global variable in
    the class. I have another button which I use as a SUBMIT button. I want to be
    able to take the values of variables that I set. However, as soon as I click
    the button, it immediately forgets all of the global variables. What am I
    missing? How come everything dissapears? What do I need to do to make it
    remember everything just prior to the submit button?


    Thanks!!! :(

    Todd
     
    =?Utf-8?B?VG9kZCBKYXNwZXJz?=, Apr 5, 2007
    #1
    1. Advertising

  2. Hi Todd

    Page variables are not stored in the viewstate automatically. You have to do
    it yourself via viewstate collection, which is serialized to a hidden field,
    and persited between postback.:

    public int MyCount
    {
    get
    {
    object value = ViewState["MyCount"];
    return value == null ? 0 /* default value */ : (int) value;
    }
    set
    {
    ViewState["MyCount"] = value;
    }
    }

    public string AnotherProperty
    {
    get
    {
    object value = ViewState["AnotherProperty"];
    return value == null ? String.Empty : (string) value;
    }
    set
    {
    ViewState["AnotherProperty"] = value;
    }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
    if (IsPostBack)
    {
    int count = MyCount;
    string value = AnotherProperty;
    }
    else
    {
    MyCount = 10;
    AnotherProperty = "I will be persited!";
    }
    }

    --
    Milosz


    "Todd Jaspers" wrote:

    > Hey guys,
    >
    > I guess I don't understand this whole Post-Back thing. I'm writing an
    > ASP page using VS2005 in C#. On the page I'm working on, I set several
    > variables on Form Load. I then have a button which dynamically builts some
    > objects. I take a COUNT of these objects and set it to a global variable in
    > the class. I have another button which I use as a SUBMIT button. I want to be
    > able to take the values of variables that I set. However, as soon as I click
    > the button, it immediately forgets all of the global variables. What am I
    > missing? How come everything dissapears? What do I need to do to make it
    > remember everything just prior to the submit button?
    >
    >
    > Thanks!!! :(
    >
    > Todd
     
    =?Utf-8?B?TWlsb3N6IFNrYWxlY2tpIFtNQ0FEXQ==?=, Apr 5, 2007
    #2
    1. Advertising

  3. Hi Todd,

    Whenever you do something causing a post-back to the server, the server renders the page again, what you need to remember is in Page_Load only initialize values if the page isn't posted back (that is, the first time the page is created).

    Page_Load(object sender, EventArgs e)
    {
    if(!IsPostBack)
    {
    // Only write hello world the first time the page is loaded
    MyTextBox.Text = "Hello World";
    }
    }

    In addition to this, the values you want to remember needs to be passed back to the server in the ViewState. The TextBox has a EnabledViewState property which should be set to true

    If this does not help, please show us your code and we might be able to give you a better answer.


    On Thu, 05 Apr 2007 15:54:01 +0200, Todd Jaspers <> wrote:

    > Hey guys,
    >
    > I guess I don't understand this whole Post-Back thing. I'm writing an
    > ASP page using VS2005 in C#. On the page I'm working on, I set several
    > variables on Form Load. I then have a button which dynamically builts some
    > objects. I take a COUNT of these objects and set it to a global variable in
    > the class. I have another button which I use as a SUBMIT button. I want to be
    > able to take the values of variables that I set. However, as soon as I click
    > the button, it immediately forgets all of the global variables. What am I
    > missing? How come everything dissapears? What do I need to do to make it
    > remember everything just prior to the submit button?
    >
    >
    > Thanks!!! :(
    >
    > Todd
    >




    --
    Happy coding!
    Morten Wennevik [C# MVP]
     
    Morten Wennevik [C# MVP], Apr 5, 2007
    #3
  4. Wow... this is very interesting Milosz,

    Thanks for the reply... I'll mess around with this and see what happens!
    THanks!!!!


    Todd



    "Milosz Skalecki [MCAD]" wrote:

    > Hi Todd
    >
    > Page variables are not stored in the viewstate automatically. You have to do
    > it yourself via viewstate collection, which is serialized to a hidden field,
    > and persited between postback.:
    >
    > public int MyCount
    > {
    > get
    > {
    > object value = ViewState["MyCount"];
    > return value == null ? 0 /* default value */ : (int) value;
    > }
    > set
    > {
    > ViewState["MyCount"] = value;
    > }
    > }
    >
    > public string AnotherProperty
    > {
    > get
    > {
    > object value = ViewState["AnotherProperty"];
    > return value == null ? String.Empty : (string) value;
    > }
    > set
    > {
    > ViewState["AnotherProperty"] = value;
    > }
    > }
    >
    > protected void Page_Load(object sender, EventArgs e)
    > {
    > if (IsPostBack)
    > {
    > int count = MyCount;
    > string value = AnotherProperty;
    > }
    > else
    > {
    > MyCount = 10;
    > AnotherProperty = "I will be persited!";
    > }
    > }
    >
    > --
    > Milosz
    >
    >
    > "Todd Jaspers" wrote:
    >
    > > Hey guys,
    > >
    > > I guess I don't understand this whole Post-Back thing. I'm writing an
    > > ASP page using VS2005 in C#. On the page I'm working on, I set several
    > > variables on Form Load. I then have a button which dynamically builts some
    > > objects. I take a COUNT of these objects and set it to a global variable in
    > > the class. I have another button which I use as a SUBMIT button. I want to be
    > > able to take the values of variables that I set. However, as soon as I click
    > > the button, it immediately forgets all of the global variables. What am I
    > > missing? How come everything dissapears? What do I need to do to make it
    > > remember everything just prior to the submit button?
    > >
    > >
    > > Thanks!!! :(
    > >
    > > Todd
     
    =?Utf-8?B?VG9kZCBKYXNwZXJz?=, Apr 5, 2007
    #4
  5. =?Utf-8?B?VG9kZCBKYXNwZXJz?=

    sloan Guest

    Just to followup with the others

    I am thinking by "global", you mean a member variable for that specific
    page.

    I'd probably try to avoid the word "global" with your identification, or you
    might get post that attacks that practice, if the poster doesn't read your
    post carefully.

    Member Variables at Page scope level are ok.

    "Global Variables" are questionable at best.
    But that's a different subject for a different day.





    "Todd Jaspers" <> wrote in message
    news:...
    > Hey guys,
    >
    > I guess I don't understand this whole Post-Back thing. I'm writing an
    > ASP page using VS2005 in C#. On the page I'm working on, I set several
    > variables on Form Load. I then have a button which dynamically builts some
    > objects. I take a COUNT of these objects and set it to a global variable

    in
    > the class. I have another button which I use as a SUBMIT button. I want to

    be
    > able to take the values of variables that I set. However, as soon as I

    click
    > the button, it immediately forgets all of the global variables. What am I
    > missing? How come everything dissapears? What do I need to do to make it
    > remember everything just prior to the submit button?
    >
    >
    > Thanks!!! :(
    >
    > Todd
     
    sloan, Apr 5, 2007
    #5
  6. Milosz, this is really helpful, however I still have one problem. I can't
    seem to be able to retrieve the data from my checkboxes. I build the check
    boxes dynamically in a table on one button click, and then once they are all
    built, the user has the option to check some or all of them. Then, there is a
    submit button. Once they click the submit button, all of those checkboxes
    dissapear and there doesn't seem to be a way for me to retrieve their data
    prior to them getting eliminated.

    What am I doing wrong???


    Todd


    "Milosz Skalecki [MCAD]" wrote:

    > Hi Todd
    >
    > Page variables are not stored in the viewstate automatically. You have to do
    > it yourself via viewstate collection, which is serialized to a hidden field,
    > and persited between postback.:
    >
    > public int MyCount
    > {
    > get
    > {
    > object value = ViewState["MyCount"];
    > return value == null ? 0 /* default value */ : (int) value;
    > }
    > set
    > {
    > ViewState["MyCount"] = value;
    > }
    > }
    >
    > public string AnotherProperty
    > {
    > get
    > {
    > object value = ViewState["AnotherProperty"];
    > return value == null ? String.Empty : (string) value;
    > }
    > set
    > {
    > ViewState["AnotherProperty"] = value;
    > }
    > }
    >
    > protected void Page_Load(object sender, EventArgs e)
    > {
    > if (IsPostBack)
    > {
    > int count = MyCount;
    > string value = AnotherProperty;
    > }
    > else
    > {
    > MyCount = 10;
    > AnotherProperty = "I will be persited!";
    > }
    > }
    >
    > --
    > Milosz
    >
    >
    > "Todd Jaspers" wrote:
    >
    > > Hey guys,
    > >
    > > I guess I don't understand this whole Post-Back thing. I'm writing an
    > > ASP page using VS2005 in C#. On the page I'm working on, I set several
    > > variables on Form Load. I then have a button which dynamically builts some
    > > objects. I take a COUNT of these objects and set it to a global variable in
    > > the class. I have another button which I use as a SUBMIT button. I want to be
    > > able to take the values of variables that I set. However, as soon as I click
    > > the button, it immediately forgets all of the global variables. What am I
    > > missing? How come everything dissapears? What do I need to do to make it
    > > remember everything just prior to the submit button?
    > >
    > >
    > > Thanks!!! :(
    > >
    > > Todd
     
    =?Utf-8?B?VG9kZCBKYXNwZXJz?=, Apr 5, 2007
    #6
  7. Todd,
    Whenever you create controls dynamically (such as by the user clicking a
    button), and the Page is posted back, you need to have a way to recreate
    these controls, with the same Id's, so that the ViewState will be able to
    hook back up to them.
    They won't "automatically" reappear when the page goes through its lifecycle
    on a postback.
    Peter
    --
    Site: http://www.eggheadcafe.com
    UnBlog: http://petesbloggerama.blogspot.com
    Short urls & more: http://ittyurl.net




    "Todd Jaspers" wrote:

    > Milosz, this is really helpful, however I still have one problem. I can't
    > seem to be able to retrieve the data from my checkboxes. I build the check
    > boxes dynamically in a table on one button click, and then once they are all
    > built, the user has the option to check some or all of them. Then, there is a
    > submit button. Once they click the submit button, all of those checkboxes
    > dissapear and there doesn't seem to be a way for me to retrieve their data
    > prior to them getting eliminated.
    >
    > What am I doing wrong???
    >
    >
    > Todd
    >
    >
    > "Milosz Skalecki [MCAD]" wrote:
    >
    > > Hi Todd
    > >
    > > Page variables are not stored in the viewstate automatically. You have to do
    > > it yourself via viewstate collection, which is serialized to a hidden field,
    > > and persited between postback.:
    > >
    > > public int MyCount
    > > {
    > > get
    > > {
    > > object value = ViewState["MyCount"];
    > > return value == null ? 0 /* default value */ : (int) value;
    > > }
    > > set
    > > {
    > > ViewState["MyCount"] = value;
    > > }
    > > }
    > >
    > > public string AnotherProperty
    > > {
    > > get
    > > {
    > > object value = ViewState["AnotherProperty"];
    > > return value == null ? String.Empty : (string) value;
    > > }
    > > set
    > > {
    > > ViewState["AnotherProperty"] = value;
    > > }
    > > }
    > >
    > > protected void Page_Load(object sender, EventArgs e)
    > > {
    > > if (IsPostBack)
    > > {
    > > int count = MyCount;
    > > string value = AnotherProperty;
    > > }
    > > else
    > > {
    > > MyCount = 10;
    > > AnotherProperty = "I will be persited!";
    > > }
    > > }
    > >
    > > --
    > > Milosz
    > >
    > >
    > > "Todd Jaspers" wrote:
    > >
    > > > Hey guys,
    > > >
    > > > I guess I don't understand this whole Post-Back thing. I'm writing an
    > > > ASP page using VS2005 in C#. On the page I'm working on, I set several
    > > > variables on Form Load. I then have a button which dynamically builts some
    > > > objects. I take a COUNT of these objects and set it to a global variable in
    > > > the class. I have another button which I use as a SUBMIT button. I want to be
    > > > able to take the values of variables that I set. However, as soon as I click
    > > > the button, it immediately forgets all of the global variables. What am I
    > > > missing? How come everything dissapears? What do I need to do to make it
    > > > remember everything just prior to the submit button?
    > > >
    > > >
    > > > Thanks!!! :(
    > > >
    > > > Todd
     
    =?Utf-8?B?UGV0ZXIgQnJvbWJlcmcgW0MjIE1WUF0=?=, Apr 5, 2007
    #7
    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. Replies:
    2
    Views:
    1,097
  2. Freddie
    Replies:
    0
    Views:
    425
    Freddie
    Jul 19, 2006
  3. chris
    Replies:
    0
    Views:
    343
    chris
    Oct 13, 2006
  4. Replies:
    15
    Views:
    3,115
  5. Noé Alejandro

    Dissapearing data from array

    Noé Alejandro, Apr 7, 2010, in forum: Ruby
    Replies:
    3
    Views:
    96
    Noé Alejandro
    Apr 7, 2010
Loading...

Share This Page