views:

223

answers:

4

hey all,

im really new to ASP.Net and still havnt got my head round most of the concepts, im really having problems with variable, when creating a desktop application, any variable created in code for example

int n = 10;

this variable will be there until the program is running right. but in asp iv created variables which are supposed to last until the user gets of that page, so for example say i have a page which takes the users value ( call this variable n say the user type in 10) and every time the user presses the next button the code add 10 to this value. so the first time the user presses the next button

n= n + 10;

what i have found is no matter how many times i press the next button n will always equal 20, because the previous value is not saved !!

these values are populated the first time the user enters that page, once the user clicks the next button the content of these values disappear! how can stop this from happening ??

hope this makes sense !!

thanks

+2  A: 

Variables don't automatically maintain state across page calls in ASP.NET. Asp.Net Page has a life cycle and being stateless, the information is lost at the end of this cycle, after a request has been served on the client side. There are several solution for this.

Here is hidden field example.


In HTML, you simply create:

<input type="hidden" id="myHiddenVar">

To set it's value in javascript:

 document.getElementById("myHiddenVar").value = "myValue"

In ASP.NET code-behind, you use the Request object to retrieve the value.

string myHiddenVar = (string)Request.Params["myHiddenVar"];

Asad Butt
+2  A: 

Each time the user requests the page, it receives the request, is initialized, processed, and then rendered (read about the Asp.Net Page Life Cycle here.). So, on each page request, your variables are being created, initialized, and then assigned your values - each page load is a new life cycle. If you want values to persist betwen page life cycles, then you need to use one of the available methods (ViewState, Session, QueryString, Post, ....) to pass the values to the "next" request.

thedugas
+3  A: 

Every time you refresh page (click also refreshes page) new instance of Page class is created. This means that all your fields become empty.

You can try to use ViewState to persist data between refreshes. But you should be care as this will increase page size. So best practice is to minimaze data in view state.

Another options is SessionState, but in this case you will keep data even between pages.

Hope this helps.

Mike Chaliy
ok say i use sessions, when the page loads up all the variable are populated right, and then all these are stored in the session right. so for example i have this jagged array protected string[][] answersJArray;which is then populated, and stored in session like so Session.Add("answersJArray", answersJArray);later on in my code i need to traverse through this jagged array, i use the following codefor (j = 0; j < answersJArray[index].Length; j++) { RadioButtonList1.Items.Add(answersJArray[index][j].ToString()); }how do i do this with the session variable ?
c11ada
later in your code get `answersJArray` from Session and work as expected. Actullay yuo do not need field `answersJArray`.
Mike Chaliy
+2  A: 

Variables in ASP.NET work exactly the way that variables in Windows Forms work:

public class MyPage : System.Web.UI.Page
{
    private int n = 0;

    public void Button_Click(object sender, EventArgs e)
    {
        n = n + 1;
    }
}

public class MyForm : System.Windows.Forms.Form
{
    private int n = 0;

    public void Button_Click(object sender, EventArgs e)
    {
        n = n + 1;
    }
}

So, why is it with the page, that the old value of "n" is gone the next time you hit the page?

Because HTTP is a request/response protocol. Each request creates a new instance of your MyPage class, each with its own copy of "n". In a Windows Forms application, you're probably not creating a new instance of your form on every button click!

John Saunders