3

hi i bind a DataTable to a gridview. i want to use the DataTable in some postback event, and ii want to use viewstate. does gridview save data in viewstate? if no what is the best way to do this.

(full story : i have a custom gridview derived from standard asp.net gridview, and i put a button in header to export data to excel, i want to use data saved in viewstate to export to excel)

thanks

asked Mar 31, 2011 at 17:02
1

3 Answers 3

6

The short answer is yes, information shown in the GridView, much like the current state of any control, is stored in ViewState. However, I do not think it is easy, if even possible, to get to this data yourself out of the ViewState collection that is available to WebControls. Instead, ASP.NET populates values from ViewState before running event handlers in the lifecycle, so if you interrogate your GridView object server-side in a handler, you should see the current values of the cells.

answered Mar 31, 2011 at 17:07
Sign up to request clarification or add additional context in comments.

1 Comment

That was pretty direct actually. He answered you question about storage in ViewState and suggested a way to retrieve the data.
4

There are other ways like:

1: Storing your DataTable in Session

2: Depending on how often that export button is going to be used and the amount of data fetched, you can make a DB call.

answered Mar 31, 2011 at 17:29

1 Comment

+1 If you need the data to be exactly the same, do #1. If you don't, do #2. Your database should be able to handle it better than your web server's session.
0

Below Statement would make the idea clear:

It Clearly shows that gridview data is stored in viewstate:

//Binding GridView
gv_Settlements.DataSource = dt_settlements;
gv_Settlements.DataBind();
//Retrieving Data
DataTable dt = (DataTable)gv_Settlements.DataSource;
answered Oct 26, 2012 at 12:25

1 Comment

If all three statements are run in the same request then dt = dt_settlements. If the first two lines are run in one request and the last line in a second and gv_Settlements.ViewStateMode = UI.ViewStateMode.Disabled then dt would be nothing.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.