Data / Values keep dissapearing!!!

G

Guest

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
 
G

Guest

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!";
}
}
 
M

Morten Wennevik [C# MVP]

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

Guest

Wow... this is very interesting Milosz,

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


Todd



Milosz Skalecki said:
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 said:
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
 
S

sloan

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

Guest

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 said:
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 said:
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
 
G

Guest

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 said:
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 said:
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 said:
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
 

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,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top