C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Communities   Interview   Jobs   Projects   Offshore Development    
Silverlight Tutorials | Mentor | Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Bookmarks | Revenue Sharing |


Prizes & Awards
My Profile



Active Members
TodayLast 7 Days more...

New Feature: Community Sites: Create your own .NET community website and start earning from Google AdSense ! It's Free !




Improve performance with the EnableViewState property


Posted Date: 12 Aug 2007    Resource Type: Articles    Category: Web Applications

Posted By: Kamal       Member Level: Gold
Rating:     Points: 15



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 If
End 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.Clone
End Sub




Responses

Author: critic    15 Aug 2007Member Level: Bronze   Points : 0
http://www.devx.com/vb2themax/Tip/18714


Feedbacks      
Popular Tags   What are tags ?   Search Tags  
(No tags found.)

Post Feedback


This is a strictly moderated forum. Only approved messages will appear in the site. Please use 'Spell Check' in Google toolbar before you submit.
You must Sign In to post a response.
Next Resource: ASP.NET OPTIMIZATION TIPS
Previous Resource: Add dynamic controls with events
Return to Discussion Resource Index
Post New Resource
Category: Web Applications


Post resources and earn money!
 
Related Resources



dotNet Slackers   BizTalk Adaptors    Web Design

SPOC

Contact Us    Privacy Policy    Terms Of Use