Comparing a Random Value with an entry problem..

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • dyte
    New Member
    • Jul 2010
    • 10

    Comparing a Random Value with an entry problem..

    Hi all, i have a simple application which i couldnt run correctly. My codes;
    --------------------------------------------------------
    Code:
    using System;
    using System.Collections.Generic;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    
    protected void Page_Load(object sender, EventArgs e)
    {
        Random r = new Random();  
        int myRandom = r.Next(1000, 9999);
        Label1.Text = myRandom.ToString();
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        string mynumber = TextBox2.Text;
        if(mynumber == Label1.Text)
            Label2.Text = "Same Numbers.";
        else
            Label2.Text = "Different Numbers.";
    }
    -------------------------------------------------------
    When i run that page, and enter the same number with label1 to textbox1 and click to the button, first label1 changes to another number and so the comparement does not match.

    Hope u can help me, thank you a lot..
    Last edited by Frinavale; Nov 8 '10, 02:26 PM. Reason: Please post code in [code] ... [/code] tags.
  • dyte
    New Member
    • Jul 2010
    • 10

    #2
    no one?! please, what is wrong here?

    Comment

    • Sandeep M

      #3
      You are new to web application rite?

      ok here pageload event will be executed each and every time (postback too). so you need to make sure Page_Load is not being executed after post back...

      change to...

      Code:
      protected void Page_Load(object sender, EventArgs e)
      {
        if (!IsPostBack)
        {
          Random r = new Random(); 
          int myRandom = r.Next(1000, 9999);
          Label1.Text = myRandom.ToString();
        }
      }
      Last edited by Frinavale; Nov 8 '10, 02:27 PM. Reason: Please post code in [code] ... [/code] tags. Added code tags and code indentation.

      Comment

      • Frinavale
        Recognized Expert Expert
        • Oct 2006
        • 9749

        #4
        Sandeep is correct.

        When developing ASP.NET applications you should be aware of how the ASP.NET Life Cycle is going to have an effect on your code.

        This is the general outline of the ASP.NET page life cycle:
        1. Browser makes a request to the server
        2. Server forwards the request to your application
        3. The Objects required to execute the page are created and loaded with data (initialized)
        4. The Page Load event fires
        5. Any "Postback Events" are fired (these events are fired by a control on the page...like a Button's Click event).
        6. The PreRender event happens (this is the last time you can access the controls on your page)
        7. The controls are rendered into HTML
        8. The HTML is sent to the browser
        9. All server objects are destroyed


        Therefore, the Page Load event occurs ever page request. If you put code in the Page Load event it will be executed every page request. In order to prevent this from happening you need to check if it is the first time the page is loading, or if the page is posting back to the server.

        You can check if the page is posting back to the server by using the Page.IsPostback property (you can drop the "Page" part when working with code within the Page object). If IsPostback is true then the page is posting back in order to handle a control's event that posted back to the server...but if IsPostback is false, it is the first time the page is loading (because no control on the page caused the request).

        -Frinny

        Comment

        • dyte
          New Member
          • Jul 2010
          • 10

          #5
          Thanks you both Sundeep M and Frinavale. But what I need is to check these numbers with postbacks. Let me explain;
          If I use the condition if(!IsPostBack) , there is only one comparing till I refresh the page. But what I need is that, when I clicked the button, it must compare the label1 with textboxt, write the result to label2 and then refresh the page.(In this case, I think PostBack may be useful because it changes my random number, and so a new comparing may be done.)
          When I am trying this, as I said, when i push the button label1 changes before comparison(I know this is because of postback! And this is the part I could not handle.)

          Briefly, i need to change label1 after comparing. If i use (!IsPostBack), it does not change.

          Thank you both again..

          Comment

          • Frinavale
            Recognized Expert Expert
            • Oct 2006
            • 9749

            #6
            Consider moving the random number generation to the PreRender event...
            This event occurs every page request, and it occurs after the postback events are executed (eg...the button click event).

            -Frinny

            Comment

            • dyte
              New Member
              • Jul 2010
              • 10

              #7
              thank you again Frinavale, i think this may work..

              Comment

              Working...