Click here to Skip to main content
15,891,607 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
I am making a "guess the number" feature for a website, but when I type a number and click "guess", the whole page reloads and generates another random number. How can I fix this problem?

C#
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 number;


    protected void Page_Load(object sender, EventArgs e)
    {
        Random genrator = new Random();
        number = genrator.Next(0, 30);
        lbl_start.Text = (" jag tänker på ett tal mellan 1-100 gissa den om du kan!!");

    }



    protected void btn_gissa_Click(object sender, EventArgs e)
    {



        int guess = Convert.ToInt32(txt_matain.Text);

        if (guess > number)
        {
            lbl_gissa.Text = ("mindre" + number);
        }
        if (guess < number)
        {
            lbl_gissa.Text = ("större");
        }

        if (guess == number)
        {
            lbl_gissa.Text = ("du gissa talet grattis!!");
        }


    }


}
Posted
Updated 31-May-13 11:38am
v2
Comments
CHill60 31-May-13 12:43pm    
Are you checking for postback?
walanw 31-May-13 12:49pm    
what? i don´t understand

Just add
C#
if (!IsPostBack){}

to the Page Load. Buttons postback causing Page_Load to run first and then the button click event runs. Just wrap your code into an if (!IsPostBack)
C#
    protected void Page_Load(object sender, EventArgs e)
    {
if (!IsPostBack)
{
        Random genrator = new Random();
        number = genrator.Next(0, 30);
        lbl_start.Text = (" jag tänker på ett tal mellan 1-100 gissa den om du kan!!");
 }
    }
 
Share this answer
 
Comments
walanw 31-May-13 13:17pm    
but if i do this the guess number will always be 0 i dont know why :S what can i do to this ?
ZurdoDev 31-May-13 13:30pm    
So, you need to generate the random number in the button click event instead.
walanw 31-May-13 13:41pm    
it does not work, how would you write this code to make it work? do i have to find another way to write it ?
ZurdoDev 31-May-13 14:10pm    
I don't know. I don't know what you want to do exactly. If you just want to compare the number they typed in vs. a random number just put it all in to the button click event. Otherwise, please be clear on what you need.
walanw 31-May-13 14:15pm    
i just want to make a working guess number game for website using asp.net c# but this way does not seem working well, do you have another way to do it so it works ? if you don¨t have time to do that its ok thank you so far :)
This type of value should be stored in Session. This code will do what you are looking for (modify it appropriately for your specific needs):
ASP.NET
<%@ Page Language="C#" %><!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
  <title>Random Number</title>
</head>
<body>
  <form id="frmMain" runat="server">
    <asp:TextBox runat="server" ID="txtGuess" />
    <asp:Button runat="server" ID="btnGuess" Text="Guess" onclick="btnGuess_Click" />
    <div>
      <asp:Label runat="server" ID="lblGuess" />
    </div>
  </form>
</body>
</html>

<script runat="server">
  protected void Page_Load(object sender, EventArgs e)
  {
    if (!Page.IsPostBack)
    {
      Random rnd = new Random((int)(DateTime.Now.Ticks % int.MaxValue));
      Session["RandomNumber"] = rnd.Next(0, 30);
    }
  }
  protected void btnGuess_Click(object sender, EventArgs e)
  {
    int guess;
    if (int.TryParse(txtGuess.Text, out guess))
    {
      int number = (int)Session["RandomNumber"];
      lblGuess.Text = number.ToString() + "_" + guess.ToString();
    }
  }
</script>
 
Share this answer
 
Comments
walanw 1-Jun-13 2:49am    
thank you for your time but i think my code work now all i need is using classes in this game
can you give an example on how a class can be if you would create class for this game?
AspDotNetDev 1-Jun-13 14:08pm    
Is this a homework assignment? If so, I suggest you ask your teacher for help in the areas you do not understand.
walanw 1-Jun-13 14:11pm    
my teacher is not that good and she dont answer the most of my questions thats why am here on this site if got the anwers from my teacher i would not come here ijust want an example on how to make and use classes with code:)
AspDotNetDev 1-Jun-13 14:15pm    
http://msdn.microsoft.com/en-us/library/vstudio/x9afc042.aspx
walanw 1-Jun-13 14:24pm    
i have read all of this for a week ago but i still have some problms understanding, like how can u add an class to code, like what do i write to add the class to my code ?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900