Improve performance with the EnableViewState property

Improve performance with the EnableViewState property

All Web Forms controls, and the page itself, expose the EnableViewState property, a boolean that says whether the current value of the control must be saved in the __VIEWSTATE hidden field and restored during a page postback.

By default this property is True, which means that a field’s value is automatically preserved between postback. This makes a Web Forms behave more similarly to a windows forms. In some cases, however, you can set this property to False and reduce the number of bytes sent along the wire. A good candidate for this arrangement are password fields, which should be cleared any time the form is sent back to the client.

Another case where you may want to set this property to False is when you are building a databound DataGrid. Typically you bind a DataGrid with this code in the Page_Load event handler:

Private Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles _    MyBase.Load    If Not Me.IsPostBack() Then        ' Create the DataReader        Dim cn As New OleDbConnection("my connection string")        Dim cmd As New OleDbCommand("SELECT * FROM MyTable", cn)        Dim dr As OleDbDataReader = cmd.ExecuteReader()        ' Bind it to the DataGrid        DataGrid1.DataSource = dr        DataGrid1.DataBind()        ' close the DataReader and the Connection        dr.Close        cn.Clone    End IfEnd Sub

The above code queries the database only once, and then restores the contents of the DataGrid from the __VIEWSTATE field, which means that a lot of data is sent to the client. In some cases you may find that rebinding the DataGrid at each page request might make your application more scalable (also because odds are that the data coming from the query are already in the database’s cache):

Private Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles _    MyBase.Load    ' Create the DataReader    Dim cn As New OleDbConnection("my connection string")    Dim cmd As New OleDbCommand("SELECT * FROM MyTable", cn)    Dim dr As OleDbDataReader = cmd.ExecuteReader()    ' Bind it to the DataGrid    DataGrid1.DataSource = dr    DataGrid1.DataBind()    ' close the DataReader and the Connection    dr.Close    cn.CloneEnd Sub

Share the Post:
Heading photo, Metadata.

What is Metadata?

What is metadata? Well, It’s an odd concept to wrap your head around. Metadata is essentially the secondary layer of data that tracks details about the “regular” data. The regular

XDR solutions

The Benefits of Using XDR Solutions

Cybercriminals constantly adapt their strategies, developing newer, more powerful, and intelligent ways to attack your network. Since security professionals must innovate as well, more conventional endpoint detection solutions have evolved

AI is revolutionizing fraud detection

How AI is Revolutionizing Fraud Detection

Artificial intelligence – commonly known as AI – means a form of technology with multiple uses. As a result, it has become extremely valuable to a number of businesses across

AI innovation

Companies Leading AI Innovation in 2023

Artificial intelligence (AI) has been transforming industries and revolutionizing business operations. AI’s potential to enhance efficiency and productivity has become crucial to many businesses. As we move into 2023, several

data fivetran pricing

Fivetran Pricing Explained

One of the biggest trends of the 21st century is the massive surge in analytics. Analytics is the process of utilizing data to drive future decision-making. With so much of