Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

C# beginner. Really basic webapplication question

Status
Not open for further replies.

shalomgod

Programmer
Jan 18, 2006
5
JP
hi below i have a snippet of a very simple piece of code i have used in a webapplication in C#
private void btnFire_Click(object sender, System.EventArgs e)
{
test++;
lblOne.Text =""+test;
}

when the button is clicked once. the number 1 shows in the label. after another click it should turn to 2, then 3 etc. however it stays at one and the label does not update any further. it works on a windows application though. can anybody explain why? thanks in advance.
 
Where do you set test = 0 initially?

You may want to set it the first time you load the page, and not after a postback.

Also: I believe you can use

lblOne.Text=test.ToString();
 
As you are a newb, you will have to think of things in an entirely new way for web.

Web is by design stateless.

This is done because a web server Cannot be responsible for remembering everyone that accesses a page indefinately.

What your seeing is your page, when posted back, is a brand new page every time. The server does not remember your previous value because after it built your page and sent it to you it threw it away.

That being said there are several ways to keep state information. I would suggest reading up on storing Session state.

for a quick change that would make your app work, you could change your code to read the number in the label then add 1 to it and reset it. In effect your Label would then be keeping track of your state.

it would look like this:

Code:
  private void btnFire_Click(object sender, System.EventArgs e)
        {
int _value = int.Parse(lblOne.Text);
_value++;

lblOne.Text = _value.toString();
}
 
The Web is a stateless environment which means it has no memory regarding variables, etc. Each time you fire an event such as you button click it will make a round trip to the server (A postback) and render an entirely new version of the page unless you utilize server controls that have ViewState enabled. You can remedy this by utlizing the Session object (Survives the life of the user session) What you are attempting to do looks as if you need it only at the page level, so holding it in Session would be overkill unless you will need to use that value at a later time over the course of the session. I would recommend using ViewState which will maintain the value for the life of the page.

You would initialize the value on Page Load maybe like this:

private void Page_Load(object sender, System.EventArgs e){ if(!IsPostBack){
ViewState["testValue"] = "0";
}
}

Then on your button click event you can recall the value like so:


private void btnFire_Click(object sender, System.EventArgs e){
int iValue = Convert.ToInt32(ViewState["testValue"]);
iValue++;
lblOne.Text = iValue;
ViewState["testValue"] = iValue;

}



John Pasko
"No matter where you go, there you are." -- Buckaroo Bonzai
 
thanks a lot guys. you really helped me out. im used to using java, but im kinda new to the whole C# webservices thing.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top