Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.NET - protected variable

If I use a protected variable, does the variable exist for the whole web application or does it get removed when the user move to other page by get or post? I do know that it is not accessible in other pages unless I use static variable, but I am curious as to if it exists for the whole application. Please let me know!

like image 452
SEA Avatar asked Nov 29 '25 01:11

SEA


2 Answers

when you move to other page and return, a new instance of your page class will be created and so all non static variables will be reset.

The value will be valid in a one request process life time (starts with request start and ends with request end)

making a variable protected, just means that this variable is access-able in inherited class. for example in asp.net you can use it in inherited class like inside your markup (because it inherits code behind class)

this is the meaning of protected variable

if you want to keep a value saved between pages you can use one of these items depending on your requirement :

  • Cookie
  • Query String
  • Session States
  • Application States
  • Cache

and ViewState keeps state variable between postback in a same page or control while it is not redirected to another page.

like image 55
Farzin Zaker Avatar answered Nov 30 '25 17:11

Farzin Zaker


protected keyword does not determine how long a variable exists nor does it determine whether it will be available in the next post back.

What you are probably looking for is state management.

Take a look at this webpage to see how you can maintain state between post backs, different pages etc.

Also take a look at this page to determine which state management feature to use in which situation.

like image 35
Ranhiru Jude Cooray Avatar answered Nov 30 '25 19:11

Ranhiru Jude Cooray



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!