Is there a way to prevent ASP.Net webapp from clearing page variables on the VB side?

I have a webapp in ASP.Net with VB code. I need a variable Listthat I declared so that it stays on while the person is on the page, but currently, when the control returns to the code, everything is cleared. I am completely new to ASP.net, so I have no idea if this is possible. Can this be done with a variable Session? It seems to me that the basic types are restricting me, but I could be wrong.

+3
source share
4 answers

In ASP.Net, every instance of your Code Behind class is created when the page loads, so your class level variables are reset.

Your best option is probably to save the list that you want to keep in the session. If this makes things easier, you can create a getter for a list that reads data from a session variable. In C #, it will look like this:

private List MyList
{
    get { return Session["ListKey"] as List; }
    set { Session["ListKey"] = value; }
}
+2
source

Take a look at the ASP.NET ViewState . You should also know that there are many errors if you use it, including bloating page size and performance issues. C # code (sry. I am not good at VB):

List<int> MyList
{
    get { return (List<int>) ViewState["mylist"]; }
    set { ViewState["mylist"] = value; }
}

Note. Remember to initialize this variable.

: , .

+3

, , , -, ViewState, , .

, , Session, .

, ViewState, , , ( viewstate) , .

+1
source

Here is the code I used with.

Private Property ChangedControls() As List(Of Control)
    Get
        Return DirectCast(Session("changedControls"), List(Of Control))
    End Get
    Set(ByVal value As List(Of Control))
        Session("changedControls") = value
    End Set
End Property

Assigning a new list to page loading is very simple.

0
source

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


All Articles