I'm not sure if that would help. I'm guessing it has something
to
do
with
calling it on page load and how the post backs work.
Sorting always does a postback, so is there a way to keep the current
gridview settings?
1) Store the sort order in ViewState
Also, make sure that any code you don't want to run on postback is
surrounded by if (!IsPostback) {...}
I use the (!IsPostback), but everytime I use Gridviews, I get an empty
dataset when I sort.
Is there a command that sets the current viewstate, to the last one?
DataView pageview = ViewState[currentView]???
GridView2.DataSource = pageview??
Getting an empty dataset in postback is by-designed because the server
won't persist anything unless you save your dataset in ViewState or
Session variable, otherwise, you'll risk re-querying the database to
rebuild your dataset in every postback. The ViewState however won't
persist object that is not serializabled, which the DataSet is, but
not the DataView. The problem with saving a dataset to a page's
ViewState is the content of the dataset is embedded in the page, which
will severely hinder performance in every postback (Imagine 1 million
rows converted to gibberish text that embedded in your page, un-
neccessarily increase the size of your page to be render in the
browser...)
I particularly don't like sorting a grid that requires postback.
Plainly put, it sucks. There are existing javascripts that would help
you do that.
So to enable sorting on the gridview is useless if you have more than 1
table?
Then most people sort on the dataTable before it's binded and have sortable
= false on the gridview.
I guess I could have a session varible of the sort column for each table.
Eventually I'll probably use AJAX, but I don't have the time to set it up
right now.
Oh, also of note, I take my data from an XML stream, so it's not tied into
the database.
Oh, also of note, I take my data from an XML stream, so it's not tied into
the database.
However method you used to populate your dataset, you'll have to
repopulate them if you don't persist your dataset in Session or
ViewState, it's your choice of implementation.
The rest is setting up break points in your code and see which objects
you cared about is still valid and which isn't. Especially check out
value of yourGrid.DataSource property in the next postback.
Page_Load(...)
{
if (!IsPostBack)
{
//Initial page load code gets execute here, only once (in most
cases)
}
else
{
//Code you want ONLY happen in post-back
}
//Code you want to happen, regardless of page loading or post back
}
Set your breakpoint in these blocks and see how they got triggered
when you perform an action on the client. I think it will be clear
then which object is available to you and which is not.
Another trick is after your page is rendered by the server, do a View
Source to view the source in the page, to see how your asp.net
controls are convert to its html equivalent by the server.
Good luck,
Quoc Linh