Problem: Value of a variable is lost on second postback

S

summer00

Hi everyone,

I found that the value of a variable(string type for example) is lost
after the aspx page postback.

E.G:


private void Page_Load(object sender, System.EventArgs e)
{
if(!IsPostBack)
abc = "xyz";
}
private void Command1_Click(object sender,System.EventArgs e)
{
Response.Write(abc);
}



I have assigned the abc value to "xyz" , but after i click the command
button, i found that the value of abc is lost. The code "
Response.Write(abc)" shows nothing .May i know what's wrong with it ?
Thx a lot

*-----------------------*
Posted at:
www.GroupSrv.com
*-----------------------*
 
E

Eliyahu Goldin

Web pages don't live between postbacks. They are built as needed when
requests arrive. There is a number of ways of making information persistent.
Session, viewstate, application cache.

Eliyahu
 
J

Juan T. Llibre

Nothing's wrong with it.

You are specifying that, upon first access (!IsPostBack)
the value of "abc" should be "xyz" and that when the page
*is* a postback ( i.e., when you post the page back by
clicking the command button ) the value of "abc" should
remain as "abc".

When the page is posted back, the condition (!IsPostBack)
is false, and the "abc" variable value will still be "abc".

If you want "abc" to always have the value "xyz",
you shouldn't specify
if(!IsPostBack)
abc = "xyz";
in the Page_Load event.



Juan T. Llibre
ASP.NET MVP
===========
 

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

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top