ASP.NET variable scope

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

int n = 10; 

this variable will be there until the program runs correctly. but in asp iv, variables are created that should last until the user receives this page, so, for example, let's say I have a page that takes the value of the user (call this variable n, say, the user type is 10) and Each time the user clicks on the next button, the code will add 10 to this value. so the first time the user clicks the next button

 n= n + 10; 

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

these values ​​are filled in when the user first visits this page, as soon as the user clicks on the next button, the contents of these values ​​disappear! how can this stop it?

hope it makes sense!

thanks

+4
source share
4 answers

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

You can try to use ViewState to save data between updates. But you have to be careful, as this will increase the page size. Therefore, it is best practice to minimize data in a view state.

Other parameters are SessionState, but in this case you will store data even between pages.

Hope this helps.

+4
source

Variables do not support automatic saving of page call state in ASP.NET. The Asp.Net page has a life cycle and without citizenship, information is lost at the end of this cycle after a request on the client side. There are several solutions for this.

Here is an example of a hidden field .


In HTML, you simply create:

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

To set a value in javascript:

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

In the ASP.NET code image, you use the Request object to get the value.

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

+2
source

Each time a user requests a page, he receives a request, is initialized, processed and then displayed (read about Asp.Net life cycle here. ). Thus, with every page request, your variables are created, initialized, and then your values ​​are assigned - each page load is a new life cycle. If you want the values ​​stored in the page life cycles, you need to use one of the available methods (ViewState, Session, QueryString, Post, ....) to pass the values ​​to the next query.

+2
source

Variables in ASP.NET work exactly the same as variables in Windows Forms:

 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 "n" value disappeared the next time the page is clicked?

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 probably don't create a new instance of your form every time you click a button!

+2
source

Source: https://habr.com/ru/post/1302182/


All Articles