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

September 19th, 2004, 11:39 PM
|
|
Registered User
|
|
Join Date: Sep 2004
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
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
|
|

September 20th, 2004, 08:16 PM
|
|
Friend of Wrox
|
|
Join Date: Jun 2003
Posts: 1,998
Thanks: 0
Thanked 3 Times in 3 Posts
|
|
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
|
|

September 21st, 2004, 09:24 PM
|
|
Registered User
|
|
Join Date: Sep 2004
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
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
|
|

September 22nd, 2004, 07:40 AM
|
|
Friend of Wrox
|
|
Join Date: Jun 2003
Posts: 1,998
Thanks: 0
Thanked 3 Times in 3 Posts
|
|
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
|
|

September 22nd, 2004, 07:45 AM
|
|
Authorized User
|
|
Join Date: Jul 2004
Posts: 22
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
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
|
|
 |