basic help needed on using members in asp pages

  • Thread starter Thread starter Francois
  • Start date Start date
F

Francois

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
 
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.
 
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
 
Back
Top