basic help needed on using members in asp pages

Discussion in 'ASP .Net' started by Francois, Nov 7, 2003.

  1. Francois

    Francois Guest

    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
     
    Francois, Nov 7, 2003
    #1
    1. Advertising

  2. 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" <> wrote in message
    news:...
    > 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
     
    Kevin Spencer, Nov 7, 2003
    #2
    1. Advertising

  3. 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!
     
    Francois Malgreve, Nov 8, 2003
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. JFCM
    Replies:
    4
    Views:
    5,780
  2. CoolPint
    Replies:
    8
    Views:
    1,028
    Jeff Schwab
    Dec 14, 2003
  3. Dave
    Replies:
    3
    Views:
    385
    tom_usenet
    Aug 10, 2004
  4. hdixon
    Replies:
    3
    Views:
    674
    hdixon
    Jul 9, 2006
  5. lovecreatesbeauty
    Replies:
    43
    Views:
    1,322
    Keith Thompson
    Feb 6, 2006
Loading...

Share This Page