在回发中保持随机数 [英] Maintain Random Number Across Postbacks
本文介绍了在回发中保持随机数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在为网站制作一个猜数字功能,但是当我输入一个数字然后点击猜测时,整个页面会重新加载并生成另一个随机数。我该如何解决这个问题呢?
使用系统;
使用 System.Collections.Generic;
使用 System.Linq;
使用 System.Web;
使用 System.Web.UI;
使用 System.Web.UI.WebControls;
public partial class _Default:System.Web.UI.Page
{
int number;
受保护 void Page_Load( object sender,EventArgs e)
{
Random genrator = new Random();
number = genrator.Next( 0 , 30 );
lbl_start.Text =( jagtänkerpåetttal mellan 1-100 gissa den om du kan !! 跨度>);
}
受保护 void btn_gissa_Click( object sender,EventArgs e)
{
int guess = Convert.ToInt32(txt_matain.Text);
if (猜测> 数字)
{
lbl_gissa.Text =( mindre + number);
}
if (guess < number)
{
lbl_gissa.Text =( större);
}
if (guess == number)
{
lbl_gissa.Text =( du gissa talet grattis !!);
}
}
}
解决方案
只需添加
if (!IsPostBack){}
页面加载
。按钮回发导致Page_Load首先运行,然后按钮单击事件运行。只需将代码包装成if(!IsPostBack)
protected void Page_Load( object sender,EventArgs e)
{
if (!IsPostBack)
{
Random genrator = new Random();
number = genrator.Next( 0 , 30 );
lbl_start.Text =( jagtänkerpåetttal mellan 1-100 gissa den om du kan !! 跨度>);
}
}
这种类型的值应该存储在Session 。此代码将执行您正在寻找的内容(根据您的特定需求进行适当修改):
<% @ Page 语言 = C# %> < !DOCTYPE html >
< html xmlns = http:/ /www.w3.org/1999/xhtml\">
< head runat = server >
< title > 随机数< / title >
< / head >
< body < span class =code-keyword>>
< 表格 id =frmMain runat = server > ID = btnGuess 文本 = 猜猜 onclick = btnGuess_Click < span class =code-keyword> / >
< asp:TextBox runat = server ID = txtGuess / >
< asp:按钮 runat = server
< div >
< asp:标签 runat = 服务器 ID = lblGuess < span class =code-attribute> / >
< / div >
< / form >
< / body >
< / html >
< script runat = server跨度> <温泉n class =code-keyword>>
protected void Page_Load ( object sender,EventArgs e)
{
if (!Page.IsPostBack )
{
Random rnd = new Random(( int )(DateTime。 Now.Ticks% int .MaxValue));
会话[ RandomNumber] = rnd.Next( 0 , 30 );
}
}
受保护 void btnGuess_Click( object sender,EventArgs e)
{
int guess;
if ( int .TryParse(txtGuess.Text, out guess))
{
int number =( int )会话[ RandomNumber];
lblGuess.Text = number.ToString()+ _ + guess.ToString( );
}
}
< / script > 跨度>
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?
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!!");
}
}
}
解决方案
Just add
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)
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!!"); } }
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):
<%@ 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>
这篇关于在回发中保持随机数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文