473,398 Members | 2,125 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,398 software developers and data experts.

basic help needed on using members in asp pages

I have the following ASP.NET page :

<!-- event.aspx -->
<%@ Page Language="C#" %>
<html>

<script runat=server>
protected int counter;
protected void OnClickMyButton(object src, EventArgs e)
{
counter++;
_message.InnerText = "You clicked the button " + counter + "
times!";
}
protected void Page_Init(object src, EventArgs e)
{
_MyButton.ServerClick += new EventHandler(OnClickMyButton);
}
</script>

<body>
<form runat=server ID="Form1">
<h2>ASP.NET event page</h2>
<p>
<input type=button id=_MyButton value="Click me!" runat=server
NAME="_MyButton"/>
</p>
<span id=_message runat=server/>
</form>
</body>
</html>
I expect that each time i click on the button, a new HTTP GET method
is sent to the server and the click event would call the
OnClickMyButton method which would increase the value of the counter
on each click.
But the counter never gets incremented, then I believe that i
misunderstand something in the mechanism, either the counter loose his
value between each click or after the first click the event does not
occur anymore or ... i don't tknow...
Please could u explain me how to keep a value between different call
of the same page? And what is the real mechanism each time that i
click on the button?

Thank you.

Francois
Nov 17 '05 #1
2 1269
First, I'm surprised that you are not reporting that you get an error every
time you run this page, as you never initialize your counter variable, and
attempt to imcrement it in your code.

Second, ASP.Net classes exists for the duration of a single HTTP request, as
HTTP is stateless. Therefore, unless you persist the value of your counter
across page requests somehow, it will never increment more than 1 above the
initialized value. Each time an ASP Page is requested, the classes in it are
rebuilt. That is the reason for such things as ViewState and Session State.
For example, if you defined your counter as below, it would persist and
increment its value:

private int counter
{
get
{
if (ViewState["counter"] == null) return 0;
return (int) ViewState["counter"];
}
set
{
ViewState["counter"] = value;
}
}

By defining "counter" as a ViewState variable, it is loaded into the Page
ViewState and passed back and forth to and from the server with each Page
Request, thereby persisting its current value across requests.

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
http://www.takempis.com
Big Things are made up of
Lots of Little Things.

"Francois" <fr**************@hotmail.com> wrote in message
news:fe**************************@posting.google.c om...
I have the following ASP.NET page :

<!-- event.aspx -->
<%@ Page Language="C#" %>
<html>

<script runat=server>
protected int counter;
protected void OnClickMyButton(object src, EventArgs e)
{
counter++;
_message.InnerText = "You clicked the button " + counter + "
times!";
}
protected void Page_Init(object src, EventArgs e)
{
_MyButton.ServerClick += new EventHandler(OnClickMyButton);
}
</script>

<body>
<form runat=server ID="Form1">
<h2>ASP.NET event page</h2>
<p>
<input type=button id=_MyButton value="Click me!" runat=server
NAME="_MyButton"/>
</p>
<span id=_message runat=server/>
</form>
</body>
</html>
I expect that each time i click on the button, a new HTTP GET method
is sent to the server and the click event would call the
OnClickMyButton method which would increase the value of the counter
on each click.
But the counter never gets incremented, then I believe that i
misunderstand something in the mechanism, either the counter loose his
value between each click or after the first click the event does not
occur anymore or ... i don't tknow...
Please could u explain me how to keep a value between different call
of the same page? And what is the real mechanism each time that i
click on the button?

Thank you.

Francois

Nov 17 '05 #2


For the inititializaton of the variable it is normal that I do not get
an error when I run the ASP.NET page, as counter is a value type and
then as is value initialized automnatically at zero by the runtime.

Second, I understand well your point about the fact that the page object
is a new object each time I call that page.
Thank you for that tho.
For exemple the first time I access the page, the counter will have the
value 0 and after I click one time will have the value 1.
If I go to an other page and after come back to that page, the value
will be 0 again(as it is a new instance of the ASP page). I click on the
button and the value is 1 again.
My only concern is that if I understand well the following: if I click a
second time on the button (without navigating to an other url first)
nothing happen, the value is one again. Then, regarding what you told
me, if an new HTTP request is sent to the server, then a new instance of
the ASP.NET page should be made and the value of the counter should be
back to zero again. As there is already a click event on the button at
the time that the new instance of the page is made, the counter get
incremented right after the Page object is instancied. Am I right? Could
you confirm me I understood this well? Is there anyway I could show that
the value of the counter was back to 0 to see that I am working with a
fresh instance of Page?

Before, I was afraid that there was some kind of caching either on
client or server side which made that nothing happened and the value
kept being 1.

Thank you for your precious help.

Francois

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 17 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Bob Morvay | last post by:
I am trying to determine how far I should go in encapsulating data. As I understand it, OO practices state to create private member variables and use these variables in your publicly accessible...
16
by: Jesse Liberty | last post by:
I am writing a new book on Visual Basic 2005, targeted at VB6 programmers, and to some degree VB.NET 1.x programmers. I'd like to sign up a (limited) number of volunteers to read the book and...
28
by: Randy Reimers | last post by:
(Hope I'm posting this correctly, otherwise - sorry!, don't know what else to do) I wrote a set of programs "many" years ago, running in a type of basic, called "Thoroughbred Basic", a type of...
43
by: Bill H | last post by:
25 years ago every computer came with some form of Basic interpreter so you could use yoru computer without having to buy more software. Is Javascript (teamed with HTML) set to become the new...
11
by: =?Utf-8?B?UGV0ZXIgSw==?= | last post by:
I am working with Visual Studio or alternately with Expression Web. I need to create about 50 aspx pages with about 1200 thumbnali images, typically arranged in three to four groups per page,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.