11 Replies - 1577 Views - Last Post: 10 September 2012 - 03:37 AM Rate Topic: -----

#1 jaArch  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 217
  • Joined: 23-March 10

Button keeps refreshing

Posted 04 September 2012 - 12:50 PM

Hi:


I have a simple web application where the user guesses a random number that is generated when the page first loads. I have the random number set up as it works well, but when I click the button it changes the number instead of staying the same. The only time the number should change is if the page is refreshed. Any ideas why this is happening for me?

Code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class _Default : System.Web.UI.Page
{
    int RandomNumber;

    protected void Page_Load(object sender, EventArgs e)
    {
        TextBox1.Focus();

        Random number = new Random();

        RandomNumber = number.Next(1, 26);
        Session["number"] = RandomNumber;

        Label1.Text = "" + RandomNumber;

    }
    protected void Button1_Click1(object sender, EventArgs e)
    {
        int num;
        bool res = Int32.TryParse(TextBox1.Text, out num);

        if (res == false)
        {
            Label1.Text = "False";
        }
        else if (res == true)
        {
            if (num > 25 || num < 1)
            {
                Label2.Text = "not in range";
            }
            else
            {
                Label2.Text = "";
            }
        }
    }
}


Is This A Good Question/Topic? 0
  • +

Replies To: Button keeps refreshing

#2 Nakor  Icon User is offline

  • Professional Lurker
  • member icon

Reputation: 445
  • View blog
  • Posts: 1,501
  • Joined: 28-April 09

Re: Button keeps refreshing

Posted 04 September 2012 - 06:30 PM

The page load event is called during every postback event. If you want something to only execute during the first page load then wrap it in a !IsPostBack block

if (!IsPostBack)
{
    Random number = new Random();
	 
    RandomNumber = number.Next(1, 26);
    Session["number"] = RandomNumber;
	 
    Label1.Text = "" + RandomNumber;
}


Was This Post Helpful? 1
  • +
  • -

#3 jaArch  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 217
  • Joined: 23-March 10

Re: Button keeps refreshing

Posted 04 September 2012 - 06:38 PM

View PostNakor, on 04 September 2012 - 06:30 PM, said:

The page load event is called during every postback event. If you want something to only execute during the first page load then wrap it in a !IsPostBack block

if (!IsPostBack)
{
    Random number = new Random();
	 
    RandomNumber = number.Next(1, 26);
    Session["number"] = RandomNumber;
	 
    Label1.Text = "" + RandomNumber;
}



Ha...thank you so much for your help. I was stuck on it for so long, lol. Thanks again!
Was This Post Helpful? 0
  • +
  • -

#4 jaArch  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 217
  • Joined: 23-March 10

Re: Button keeps refreshing

Posted 04 September 2012 - 10:14 PM

Now I can't seem to get it to display the error message when the user enters a number greater than 25 or less than 1. It just prints out "incorrect guess" instead of "not in range"

Is there a reason for this and any help I can get with this issue I'm having?

Thanks

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class _Default : System.Web.UI.Page
{
    int RandomNumber;
    Random number = new Random();

    protected void Page_Load(object sender, EventArgs e)
    {
        int RandomNumber;
        TextBox1.Focus();

        if (!IsPostBack)
        {
            Random number = new Random();

            RandomNumber = number.Next(1, 26);
            Session["number"] = RandomNumber;

            Label1.Text = "" + RandomNumber;
        }

    }
    protected void Button1_Click1(object sender, EventArgs e)
    {
        int num;
        int randnu;
        randnu = int.Parse(Label1.Text);
        bool res = Int32.TryParse(TextBox1.Text, out num);

        if (res == false)
        {
            Label2.Text = "False";
        }
        if (res == true)
        {
            if (num > 25 || num < 1)
            {
                Label2.Text = "not in range";
            }

            if (num == randnu)
            {
                Label2.Text = "correct guess";
            }
                if (num != randnu)
                {
                    Label2.Text = "incorrect guess";
                }
            }
        }
    }


Was This Post Helpful? 0
  • +
  • -

#5 Nakor  Icon User is offline

  • Professional Lurker
  • member icon

Reputation: 445
  • View blog
  • Posts: 1,501
  • Joined: 28-April 09

Re: Button keeps refreshing

Posted 05 September 2012 - 04:05 AM

Try this

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class _Default : System.Web.UI.Page
{
    int RandomNumber;
    Random number = new Random();

    protected void Page_Load(object sender, EventArgs e)
    {
        int RandomNumber;
        TextBox1.Focus();

        if (!IsPostBack)
        {
            Random number = new Random();

            RandomNumber = number.Next(1, 26);
            Session["number"] = RandomNumber;

            Label1.Text = "" + RandomNumber;
        }

    }
    protected void Button1_Click1(object sender, EventArgs e)
    {
        int num;
        int randnu;
        randnu = int.Parse(Label1.Text);
        bool res = Int32.TryParse(TextBox1.Text, out num);

        if (res == false)
        {
            Label2.Text = "False";
        }
        if (res == true)
        {
            if (num > 25 || num < 1)
            {
                Label2.Text = "not in range";
            }
            else if (num == randnu)
            {
                Label2.Text = "correct guess";
            }
            else if (num != randnu)
            {
                    Label2.Text = "incorrect guess";
            }
        }
    }




Was This Post Helpful? 1
  • +
  • -

#6 jaArch  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 217
  • Joined: 23-March 10

Re: Button keeps refreshing

Posted 05 September 2012 - 07:13 PM

I have the count++ setup so that it tracks how many times the user entered a valid guess if the number entered was not out of range.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

        int randomNum;
        btnNew.Visible = false;
        
        txtGuess.Focus();
        
        if (!IsPostBack)
        {
            Random number = new Random();

            randomNum = number.Next(1, 26);
            Session["number"] = randomNum;
            lblNum.Text = "" + randomNum;
            lblNum.Visible = false;
        }
    }
    protected void btnGuess_Click(object sender, EventArgs e)
    {
            int num;
            int rand;
            int count = 0;

            bool guess = Int32.TryParse(txtGuess.Text, out num);
            rand = int.Parse(lblNum.Text);

            if (guess == false)
            {
                lblError.Text = "Invalid character. Must be a number";
            }

            if (guess == true)
            {
                if (num > 25 || num < 1)
                {
                    lblError.Text = "Number is not in range. Must be between 1 and 25.";
                }

                else if (num < 26 || num > 1)
                {

                    count++;
                
                    if (num == rand)
                    {
                        lblError.Text = "Correct guess! It took you " + count + " guesses";
                        txtGuess.ReadOnly = true;
                        btnNew.Visible = true;
                        btnGuess.Enabled = false;
                        btnGiveup.Enabled = false;

                    }

                    else if (num != rand)
                    {
                        lblError.Text = "Incorrect guess";
                        if (num < rand)
                        {
                            lblError.Text = "Guess higher";
                        }
                        else if (num > rand)
                        {
                            lblError.Text = "Guess lower";
                        }
                    }
                }
            }
}
    protected void btnGiveup_Click(object sender, EventArgs e)
    {
        int rand;
        rand = int.Parse(lblNum.Text);
        txtGuess.ReadOnly = true;
        btnNew.Visible = true;
        btnGuess.Enabled = false;
        btnGiveup.Enabled = false;

        lblError.Text = "Correct number was: " + rand;
    }
    protected void btnNew_Click(object sender, EventArgs e)
    {
        int randomNum;
        Random number = new Random();

        randomNum = number.Next(1, 26);
        Session["number"] = randomNum;
        lblNum.Text = "" + randomNum;


        lblError.Text = "";
        txtGuess.ReadOnly = false;
        txtGuess.Text = "";
        btnNew.Visible = false;
        btnGuess.Enabled = true;
        btnGiveup.Enabled = true;
    }
}


When I run it and test it, the count is always shown as 1 - even when I know I entered in more than the 1 guess. It should be counting he amount of times I enter a value from 1-25, but for some reason it's not - it just continues to display it as 1.

Also, I'm wondering, is it possible to get the random number value so I can use it in the onclick button without having to place it in the label and having the label hidden? I'm not sure how else I can access the randomnumber to use when comparing it with the guess.

Thanks
Was This Post Helpful? 0
  • +
  • -

#7 rgfirefly24  Icon User is offline

  • D.I.C Lover
  • member icon


Reputation: 285
  • View blog
  • Posts: 1,503
  • Joined: 07-April 08

Re: Button keeps refreshing

Posted 06 September 2012 - 06:18 AM

If you need something to persist it needs to be stored somewhere like Session, ViewState, hidden field, or somewhere other then a variable. Unlike Application programming variables are dumped and recreated when ever the page is refreshed.
Was This Post Helpful? 0
  • +
  • -

#8 jaArch  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 217
  • Joined: 23-March 10

Re: Button keeps refreshing

Posted 06 September 2012 - 01:51 PM

View Postrgfirefly24, on 06 September 2012 - 06:18 AM, said:

If you need something to persist it needs to be stored somewhere like Session, ViewState, hidden field, or somewhere other then a variable. Unlike Application programming variables are dumped and recreated when ever the page is refreshed.


I have it in a session, but how do I use that session so I can use it the random number elsewhere?
Was This Post Helpful? 0
  • +
  • -

#9 jaArch  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 217
  • Joined: 23-March 10

Re: Button keeps refreshing

Posted 09 September 2012 - 07:12 PM

Now it seems that the count will not increment more than once - after I enter a valid guess which is between 1 and 25, it continually stays at 1 - I'm not sure why. Any help here?

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class _Default : System.Web.UI.Page
{

    
    protected void Page_Load(object sender, EventArgs e)
    {

        int randomNum;
        btnNew.Visible = false;
        
        txtGuess.Focus();
        
        if (!IsPostBack)
        {
            Random number = new Random();

            randomNum = number.Next(1, 26);
            Session["number"] = randomNum;
            lblNum.Text = "" + randomNum;
        }
    }
    protected void btnGuess_Click(object sender, EventArgs e)
    {
            int count = 0;
            int random = (int)Session["number"];  
            int num;

            bool guess = Int32.TryParse(txtGuess.Text, out num);
           

            if (guess == false)
            {
                lblError.Text = "Invalid character. Must be a number";
            }

            if (guess == true)
            {
                if (num > 25 || num < 1)
                {
                    lblError.Text = "Number is not in range. Must be between 1 and 25.";
                }

                else if (num < 26 || num > 1)
                {
                    Session["Count"] = count;

                    if (Session["Count"] == null)
                    {
                        count = 0;
                    }
                    else
                    {
                        count = (int)Session["Count"];
                        count++;
                        ["Count"] = count;
                    }

                    if (Sessionnum == random)
                    {
                        lblError.Text = "Correct guess! It took you " + count + " guesses";
                        txtGuess.ReadOnly = true;
                        btnNew.Visible = true;
                        btnGuess.Enabled = false;
                        btnGiveup.Enabled = false;

                    }

                    else if (num != random)
                    {
                        lblError.Text = "Incorrect guess";
                        if (num < random)
                        {
                            lblError.Text = "Guess higher";
                        }
                        else if (num > random)
                        {
                            lblError.Text = "Guess lower";
                        }
                    }
                }
            }
            
}
    protected void btnGiveup_Click(object sender, EventArgs e)
    {

        int random = (int)Session["number"]; 

        txtGuess.ReadOnly = true;
        btnNew.Visible = true;
        btnGuess.Enabled = false;
        btnGiveup.Enabled = false;

        lblError.Text = "Correct number was: " + random;
    }
    protected void btnNew_Click(object sender, EventArgs e)
    {
        int randomNum;
        Random number = new Random();

        randomNum = number.Next(1, 26);
        Session["number"] = randomNum;
        lblNum.Text = "" + randomNum;


        lblError.Text = "";
        txtGuess.ReadOnly = false;
        txtGuess.Text = "";
        btnNew.Visible = false;
        btnGuess.Enabled = true;
        btnGiveup.Enabled = true;
    }
}

This post has been edited by jaArch: 09 September 2012 - 07:14 PM

Was This Post Helpful? 0
  • +
  • -

#10 rgfirefly24  Icon User is offline

  • D.I.C Lover
  • member icon


Reputation: 285
  • View blog
  • Posts: 1,503
  • Joined: 07-April 08

Re: Button keeps refreshing

Posted 09 September 2012 - 08:12 PM

on line 62 you're not resetting the session correctly

it should be
Session["Count"] = count;


not
["Count"] = count;



also you're setting the Session["Count"] variable equal to something on line 52 then checking it for null on line 54. This check will always be false because you just assigned something to Session["Count"]. You can remove the whole if section. This is also why you're session is always 0 because you're setting count equal to zero on line 31 then setting it equal to the session on line 52. Remove the assignment on line 31 and the assignment on line 52 and you should be fine.

also you can shortcut testing a boolean value by just putting the variable name.

EX:

Boolean someBoolean = true;

if(someBoolean)
{
}


This post has been edited by rgfirefly24: 09 September 2012 - 08:16 PM

Was This Post Helpful? 1
  • +
  • -

#11 jaArch  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 217
  • Joined: 23-March 10

Re: Button keeps refreshing

Posted 09 September 2012 - 10:44 PM

Now I seem to be getting this error message and I have no idea why and I have no idea how to fix it:


Object reference not set to an instance of an object.

It points to line 53 for this error.


using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class _Default : System.Web.UI.Page
{


    protected void Page_Load(object sender, EventArgs e)
    {

        int randomNum;
        btnNew.Visible = false;

        txtGuess.Focus();

        if (!IsPostBack)
        {
            Random number = new Random();

            randomNum = number.Next(1, 26);
            Session["number"] = randomNum;
            //lblNum.Text = "" + randomNum;
        }
    }
    protected void btnGuess_Click(object sender, EventArgs e)
    {

        int random = (int)Session["number"];
        int num;

        bool guess = Int32.TryParse(txtGuess.Text, out num);


        if (guess == false)
        {
            lblError.Text = "Invalid character. Must be a number";
        }

        if (guess == true)
        {
             if (num > 25 || num < 1)
            {
                lblError.Text = "Number is not in range. Must be between 1 and 25.";
            }

             else if (num < 26 || num > 1)
            {
                // int count;
                int count = (int)Session["Count"];
                count++;
                Session["Count"] = count;

            }

               if (num == random)
            {
                lblError.Text = "Correct guess! It took you " + Session["Count"] + " guesses";
                txtGuess.ReadOnly = true;
                btnNew.Visible = true;
                btnGuess.Enabled = false;
                btnGiveup.Enabled = false;

                Session["Count"] = 0;

            }

              else  if (num != random)
            {
                lblError.Text = "Incorrect guess";
                  if (num < random)
                {
                    lblError.Text = "Guess higher";
                }
                  else if (num > random)
                {
                    lblError.Text = "Guess lower";
                }
            }
        }
    }

    protected void btnGiveup_Click(object sender, EventArgs e)
    {

        int random = (int)Session["number"];

        txtGuess.ReadOnly = true;
        btnNew.Visible = true;
        btnGuess.Enabled = false;
        btnGiveup.Enabled = false;

        lblError.Text = "Correct number was: " + random + ". Number of valid guesses made: " + Session["Count"];
    }
    protected void btnNew_Click(object sender, EventArgs e)
    {
        int randomNum;
        Random number = new Random();

        randomNum = number.Next(1, 26);
        Session["number"] = randomNum;
        //lblNum.Text = "" + randomNum;

        Session["Count"] = 0;

        lblError.Text = "";
        txtGuess.ReadOnly = false;
        txtGuess.Text = "";
        btnNew.Visible = false;
        btnGuess.Enabled = true;
        btnGiveup.Enabled = true;
    }
}


Was This Post Helpful? 0
  • +
  • -

#12 Nakor  Icon User is offline

  • Professional Lurker
  • member icon

Reputation: 445
  • View blog
  • Posts: 1,501
  • Joined: 28-April 09

Re: Button keeps refreshing

Posted 10 September 2012 - 03:37 AM

You're not initializing the Session["Count"] variable anywhere. Change your page load to look like this:

if (!IsPostBack)
{
    Random number = new Random();
    randomNum = number.Next(1, 26);
    Session["number"] = randomNum;
    Session["Count"] = 0;
    //lblNum.Text = "" + randomNum;
}


This post has been edited by Nakor: 10 September 2012 - 03:39 AM

Was This Post Helpful? 1
  • +
  • -

Page 1 of 1