Should I store the database ID field in the ViewState?

I need to get a record from the database, display it on a web page (I use ASP.NET), but somewhere I need to save the identifier (primary key) from this record in order to return this identifier to the database later (possibly for updating) .

I know that there may be several ways to do this, such as storing an identifier in a ViewState or a hidden field, but what is the best method and what are the reasons why I can choose this method for any others?

+3
source share
7 answers

It depends.

, - ? , , viewstate ; .

, - ? , ( CSRF )

, , , ( )? viewstate enableViewStateMac = "true" ( )

, , ? , web.config.

<pages enableViewState="true" enableViewStateMac="true" />
<machineKey ... validation="3DES" />
+6

, ? , id - 1,1 , , . ( ), ( ).

, , ( , ) , , , , . , , ( , (, 5 ) - .

, viewstate , , "". , viewstate . , .

, ,

public int CustomerID {
    get { return ViewState("CustomerID"); }
    set { ViewState("CustomerID") = value; }
}

    Public Property CustomerID() As Integer
        Get
            Return ViewState("CustomerID")
        End Get
        Set(ByVal value As Integer)
            ViewState("CustomerID") = value
        End Set
    End Property

, ViewState , , "Page.CustomerID".

+2

ViewState - . , . , Session.

, , , .

ViewState , , .

0

, - . , .

, viewstate .

, , , . ? "" , cookie. ( : -, , cookie .)

0

querystring, , .

0
Session["MyId"]=myval;

, , , viewstate

-1

,

 <asp:label runat=server id=lblThingID visible=false />
-1

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


All Articles