7 Replies - 1898 Views - Last Post: 10 April 2013 - 09:37 AM Rate Topic: -----

#1 superkb10  Icon User is offline

  • D.I.C Regular

Reputation: 29
  • View blog
  • Posts: 298
  • Joined: 27-November 11

Run function one time on Page start

Posted 07 April 2013 - 06:26 PM

Hi, so I'm writing a simple website that just generates a math problem and the user is supposed to solve it. Right now though, I've run into a problem where I can't get the the first problem to generate correctly, because I've tried generating it in the Page_Load method but then when I want the user to submit the answer with a button click, the Page_Load method is run again so that the problem is changed. How can I make it so that I have a piece of code that runs just once while the one page is loading up? I've looked online and the only one I've found remotely close is the Application_Start method but I'm under the impression that the Application_Start method should run when the website starts up, not just an individual page. Does anyone know of an event or method that I can put my code in such that the problem is only generated once. I'm using C# by the way. Thanks!

Is This A Good Question/Topic? 0
  • +

Replies To: Run function one time on Page start

#2 trevster344  Icon User is offline

  • The Peasant
  • member icon

Reputation: 224
  • View blog
  • Posts: 1,505
  • Joined: 16-March 11

Re: Run function one time on Page start

Posted 07 April 2013 - 09:46 PM

Try this one out. Though you'll have to convert to C# code.

If IsPostback = False Then
'''Place problem generator here
End if


This post has been edited by trevster344: 08 April 2013 - 08:06 AM

Was This Post Helpful? 1
  • +
  • -

#3 superkb10  Icon User is offline

  • D.I.C Regular

Reputation: 29
  • View blog
  • Posts: 298
  • Joined: 27-November 11

Re: Run function one time on Page start

Posted 08 April 2013 - 06:56 PM

Thanks! It really helped me out. I still had a small problem with the fact that all the variables are cleared, so what I'm doing now is storing the number values as strings in a label's text and then parsing it when I need the number values. If there is a better way to do this, please let me know.
Was This Post Helpful? 0
  • +
  • -

#4 trevster344  Icon User is offline

  • The Peasant
  • member icon

Reputation: 224
  • View blog
  • Posts: 1,505
  • Joined: 16-March 11

Re: Run function one time on Page start

Posted 08 April 2013 - 07:14 PM

Create a module and declare the variables there, they should stay throughout the life of the asp session / application. If that doesn't work add them to the application as an application variable

If application("var1") is nothing then
Dim string1 as string = "hello"
Application.add("var1", string1)
Else
dim mystring as string = application("var1").tostring
End if



Not sure if I did it correctly not at my compiler but lookup storing variables or data in application variables.

If you need any further help let me know.

This post has been edited by trevster344: 09 April 2013 - 10:29 AM

Was This Post Helpful? 0
  • +
  • -

#5 McSick  Icon User is offline

  • D.I.C Head

Reputation: 33
  • View blog
  • Posts: 179
  • Joined: 02-September 10

Re: Run function one time on Page start

Posted 10 April 2013 - 09:06 AM

Since you are using C#, I suggest looking into ViewState and Session for Asp.net.

ViewState
Anything stored persists to a single page only. For example if you had a page onepage.aspx, any variables in the viewstate will stay on postbacks. However moving to anotherpage.aspx and then back and all the variables would disappear.

Session
All session variables persist across website pages for a given session. So in the example above going from onepage.aspx to anotherpage.aspx and then back to onepage.aspx and session variables are stored. However going from another domain back to your domain and session variables are lost.


More information on ViewState
http://msdn.microsof...y/ms972976.aspx
And on Session
http://msdn.microsof...(v=vs.100).aspx

Both have example code that go along with it!
Was This Post Helpful? 0
  • +
  • -

#6 trevster344  Icon User is offline

  • The Peasant
  • member icon

Reputation: 224
  • View blog
  • Posts: 1,505
  • Joined: 16-March 11

Re: Run function one time on Page start

Posted 10 April 2013 - 09:10 AM

That's also why I suggested Application variables because as long as the page is in the directory of the application it will always maintain the variables.
Was This Post Helpful? 0
  • +
  • -

#7 McSick  Icon User is offline

  • D.I.C Head

Reputation: 33
  • View blog
  • Posts: 179
  • Joined: 02-September 10

Re: Run function one time on Page start

Posted 10 April 2013 - 09:33 AM

View Posttrevster344, on 10 April 2013 - 10:10 AM, said:

That's also why I suggested Application variables because as long as the page is in the directory of the application it will always maintain the variables.


You could use them however I think they seem to be out of the scope needed. What I got from his question is that he is generating a math problem every time he visits the page (maybe at random?) and thus the values and correct answer need only be saved on this one page on this visit. In that sense a ViewState would make more sense since it would persist across the postbacks but once the page is not a postback (coming back to answer a new problem?) he would generate and save those variables in the viewstate to be used again since they are potentially new. It depends on what exactly he is trying to do.
Was This Post Helpful? 1
  • +
  • -

#8 trevster344  Icon User is offline

  • The Peasant
  • member icon

Reputation: 224
  • View blog
  • Posts: 1,505
  • Joined: 16-March 11

Re: Run function one time on Page start

Posted 10 April 2013 - 09:37 AM

No doubt about it, I really think everyone should just know their options. They all are usable as easily as sessionstate, or viewstate but like you said they serve different scopes.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1