Wrox Programmer Forums
Go Back   Wrox Programmer Forums > ASP.NET and ASP > ASP.NET 2.0 > ASP.NET 2.0 Basics
| Search | Today's Posts | Mark Forums Read
ASP.NET 2.0 Basics If you are new to ASP or ASP.NET programming with version 2.0, this is the forum to begin asking questions. Please also see the Visual Web Developer 2005 forum.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 2.0 Basics section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old September 19th, 2004, 11:39 PM
Registered User
 
Join Date: Sep 2004
Location: , , .
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default Can't fire button event more than 1 time

Hi all,

I have a web form, and my page is coded in c#.
There is a button and a label in my webform, when I click on the button, the label should display +1.

My code as below:

protected void Button_Submit_OnClick (Object Src, EventArgs E) {
        Label1.Text = "Total: " + (x++);
}

x is public variable in the page. The problem is this event only fire at the first time...and seems not firing at all at the second time.

Any clue? :)

Thanks!

regards,
vic

Freedom is a Right
 
Old September 20th, 2004, 08:16 PM
Friend of Wrox
 
Join Date: Jun 2003
Location: Harrisburg, PA, USA.
Posts: 1,998
Thanks: 0
Thanked 3 Times in 3 Posts
Default

Hey,

You should be able to. It may actually not be retaining the total value... Are you storing the public variable x in the session or cache, and reloading it? After the page completely loads, the Unload event runs and all variables/objects are destroyed.

Brian
 
Old September 21st, 2004, 09:24 PM
Registered User
 
Join Date: Sep 2004
Location: , , .
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi bmains,

Thanks for the reply, I tried ViewState["xx"], and it can't work, simply having syntax error.
Anyway I solved the problem by declaring the static variable.

However, new problem arise, the static variable remain static when I come back to the same page with other refference id, it shall be cleared and reset... any clue on that? :)

Freedom is a Right
 
Old September 22nd, 2004, 07:40 AM
Friend of Wrox
 
Join Date: Jun 2003
Location: Harrisburg, PA, USA.
Posts: 1,998
Thanks: 0
Thanked 3 Times in 3 Posts
Default

Hey,

Web is a disconnected environment. You need to store the value in the viewstate or cache. When a page finishes loading, all objects and variables (even static) are destroyed. The only way to persist these is through viewstate, session, application, or cache.

When dealing with the viewstate, make sure you assign a value to the key first before retrieving the value; otherwise, you are likely to get an error:

Page_Load {
//Do it here because viewstate entry must exist before being used
ViewState["x"] = 0;
}

Brian
 
Old September 22nd, 2004, 07:45 AM
Authorized User
 
Join Date: Jul 2004
Location: , , .
Posts: 22
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Pages are instanciated and destroyed on each request. There is a classic pattern you can follow. Declare a property like this

int X
{
 get {
   if(ViewState["KEEPIT"] == null) { return X = 0; }
 else { return (int) ViewState["KEEPIT"]; }
}
set
{
  ViewState["KEEPIT"] = value;
}
}

I recommend you read a good book on ASP.net. You can going to hit pbs of this nature if you do not have some idea on the working of ASP.Net

Cheers





Similar Threads
Thread Thread Starter Forum Replies Last Post
Unable to fire a click event rfinks ASP.NET 1.0 and 1.1 Basics 2 September 1st, 2006 10:26 PM
Browser back button makes wrong event fire Surre ASP.NET 1.0 and 1.1 Professional 1 March 13th, 2006 03:40 PM
ImageButton wont fire event Moonie112 ASP.NET 1.0 and 1.1 Basics 1 February 11th, 2006 01:01 PM
SelectedIndexChanged Event doesn't fire ddudley3 ASP.NET 2.0 Professional 7 February 6th, 2006 10:47 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.