473,472 Members | 1,747 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Comparing a Random Value with an entry problem..

10 New Member
Hi all, i have a simple application which i couldnt run correctly. My codes;
--------------------------------------------------------
Expand|Select|Wrap|Line Numbers
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Web;
  4. using System.Web.UI;
  5. using System.Web.UI.WebControls;
  6.  
  7. protected void Page_Load(object sender, EventArgs e)
  8. {
  9.     Random r = new Random();  
  10.     int myRandom = r.Next(1000, 9999);
  11.     Label1.Text = myRandom.ToString();
  12. }
  13. protected void Button1_Click(object sender, EventArgs e)
  14. {
  15.     string mynumber = TextBox2.Text;
  16.     if(mynumber == Label1.Text)
  17.         Label2.Text = "Same Numbers.";
  18.     else
  19.         Label2.Text = "Different Numbers.";
  20. }
-------------------------------------------------------
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..
Nov 4 '10 #1

✓ answered by Frinavale

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

6 1601
dyte
10 New Member
no one?! please, what is wrong here?
Nov 4 '10 #2
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...

Expand|Select|Wrap|Line Numbers
  1. protected void Page_Load(object sender, EventArgs e)
  2. {
  3.   if (!IsPostBack)
  4.   {
  5.     Random r = new Random(); 
  6.     int myRandom = r.Next(1000, 9999);
  7.     Label1.Text = myRandom.ToString();
  8.   }
  9. }
Nov 5 '10 #3
Frinavale
9,735 Recognized Expert Moderator Expert
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
Nov 8 '10 #4
dyte
10 New Member
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..
Nov 8 '10 #5
Frinavale
9,735 Recognized Expert Moderator Expert
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
Nov 8 '10 #6
dyte
10 New Member
thank you again Frinavale, i think this may work..
Nov 9 '10 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Brandon Michael Moore | last post by:
I'm trying to test a web application using a tool written in python. I would like to be able to generate random values to put in fields. I would like to be able to generate random dates (in a...
5
by: JJ | last post by:
I have a MainForm/subform1/subform2 setup that I thought was working but maybe I just hadn't tested it enough or broke it as I added more features. The database has 2 main tables: Companies and...
3
by: Shapper | last post by:
Hello, I declared, in my aspx.vb code, a variable of type integer. How can I set its value to a random value which can be only 1,2,3 or 4? Thanks, Miguel
19
by: Dennis | last post by:
I have a public variable in a class of type color declared as follows: public mycolor as color = color.Empty I want to check to see if the user has specified a color like; if mycolor =...
0
by: yp.yean | last post by:
Hi, I created a custom control, and encountered a dirty property value persistence problem. I created a property with a custom class type, call SQLSettings which holds the SQL connection...
2
by: Pugi! | last post by:
hi, I am using this code for checking wether a value (form input) is an integer and wether it is smaller than a given maximum and greater then a given minimum value: function...
0
by: thangs1 | last post by:
C# Code: for(int a =0;a< (some array);a++) { } variable a is assigned a random value. Then i deleted that variable and again typed. Now it assigns correctly.Whats the reason for this in...
2
by: ahmurad | last post by:
Dear all, I am fresh php programmer. I've spent much time to solve a checkbox related null value submission problem in php platform. I want to submit 4 checkbox value. if I checked all the...
3
by: momogi | last post by:
I'm making a TSP (Travelling Salesman Problem) project in Simulated Annealing Method. Here, I need to add a condition to the generated random value. I wanna ask you, how to generate two random...
2
by: Cosmic programmer | last post by:
I need to generate a random value from x to y having increment i. where x is start value, y is end value. so if x = 2 and y = 12 and i = 2 then random value should be generated from the...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.