By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
431,966 Members | 2,054 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 431,966 IT Pros & Developers. It's quick & easy.

Problem: Value of a variable is lost on second postback

P: n/a
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
*-----------------------*
Nov 19 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
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

"summer00" <ka******@yahoo-dot-com.no-spam.invalid> wrote in message
news:41**********@Usenet.com...
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
*-----------------------*

Nov 19 '05 #2

P: n/a
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
===========
"summer00" <ka******@yahoo-dot-com.no-spam.invalid> wrote in message
news:41**********@Usenet.com... 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
*-----------------------*

Nov 19 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.