Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > Data / Values keep dissapearing!!!

Reply
Thread Tools

Data / Values keep dissapearing!!!

 
 
=?Utf-8?B?VG9kZCBKYXNwZXJz?=
Guest
Posts: n/a
 
      04-05-2007
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
 
Reply With Quote
 
 
 
 
=?Utf-8?B?TWlsb3N6IFNrYWxlY2tpIFtNQ0FEXQ==?=
Guest
Posts: n/a
 
      04-05-2007
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

 
Reply With Quote
 
 
 
 
Morten Wennevik [C# MVP]
Guest
Posts: n/a
 
      04-05-2007
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 <(E-Mail Removed)> 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]
 
Reply With Quote
 
=?Utf-8?B?VG9kZCBKYXNwZXJz?=
Guest
Posts: n/a
 
      04-05-2007
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

 
Reply With Quote
 
sloan
Guest
Posts: n/a
 
      04-05-2007

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" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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



 
Reply With Quote
 
=?Utf-8?B?VG9kZCBKYXNwZXJz?=
Guest
Posts: n/a
 
      04-05-2007
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

 
Reply With Quote
 
=?Utf-8?B?UGV0ZXIgQnJvbWJlcmcgW0MjIE1WUF0=?=
Guest
Posts: n/a
 
      04-05-2007
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

 
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
How keep python socket alive for ever by setting Keep alive flag. hisan Python 1 06-25-2012 05:30 PM
Pop-up gives old values - like memory is not cleared and same values keep appearing chris ASP .Net 0 10-13-2006 11:57 AM
JavaScript: how to keep the variable values when page posted back =?Utf-8?B?ZGF2aWQ=?= ASP .Net 2 03-31-2006 02:54 PM
Keep URL in sync with selected control values Matthias S. ASP .Net 0 11-24-2005 04:37 PM
asp form with interdependant select boxes doesn't keep values selected, when user returns to it Galina ASP .Net 0 12-15-2003 12:51 PM



Advertisments