Datagrid Girl

Marcie, ASP.NET Datagrid Blogger Girl

Datagrid Tip of the Day: ASP.NET Datagrids and ViewState

I’ve answered this twice in the last 24 hours, so I figure it’s worthy of a blog post.  If you disable ViewState on a page with an ASP.NET Datagrid, and you want to use events—sorting, editing, or paging—then you need to .DataBind() the grid on every Page_Load.  Take out that if (!IsPostBack) or If Not IsPostBack Then line that you’ve seen in the examples, and normally is appropriate on a page that uses ViewState.  Now what this means is that you end up databinding twice, so make sure to save off your DataSource to a page-level variable so that you don’t retrieve the data twice.

I mention this technique in my MSDN article, Common Datagrid Mistakes.  Maybe one day I’ll do a whole article exclusively on Datagrids and ViewState.

--Datagrid Girl

Comments

Blanca Guerra said:

Hi!!

I just want to say that i'll be waiting for that article!

I'm trying to order a report with datagrid filled programatically (with %) and i think that view state it's the solution, but i don't know how, yet.
# March 19, 2004 8:33 PM

Saravana Kumar said:

Other way to reduce the viewstate size and to have all postback events is keeping enableviewstate ON at datagrid level but disabling viewstate at datagrid item level.I have explained about this in this article,

http://www.extremeexperts.com/Net/Articles/ViewState.aspx

# April 1, 2004 1:53 AM
Leave a Comment

(required) 

(required) 

(optional)

(required)