GridView Sorting without losing user modifications - asp.net

I have a bit of a unique problem. I am using a GridView to display information from a database. The requirements for this GridView are that it is always editable so I have TextBoxes in the ItemTemplates and want to save the values when a button is clicked.
That all works great.
The problem is I am trying to get sorting (and eventually paging) working and hitting problems. If I bind the GridView from an ObjectDataSource this obviously doesn't work because it rebinds the datasource before it sorts and I lose any modified values. If I bind the GridView from a DataTable and try to store it in a Session variable it still doesn't work because any modified values aren't in the session variable.
I'm really just looking for suggestions on what I could possibly do to sort (and page later) without losing values the user has modified. I realize most people don't modify the Gridview all at once so its a weird problem.

You could implement client-side sorting of the GridView which would have the added benefit of making your UI feel faster. There is a blog post on sorting with jQuery you could try, though I have not personally used the method suggested.
Alternatively, you could send the values back to the server via AJAX while editing and save them (temporarily) in the session, the database, or where it makes the post sense in your scenario. You would obviously need to retrieve them when sorting.
One final option would be to save your values on the postback caused by the sort command. Mostly you would do something along the lines of handling the OnTextChanged event of the TextBoxes and (as above) saving the values to a temporary store. You would not want these TextBoxes to autopostback - they would simply wait for a postback such as the one caused by your sorting event. In this scenario you could also use the temporary store you populate during the save event to avoid duplicating the effort.

Related

Best Way to Postback Dynamic Table

I have a server-side table control. Since I don't know how many rows and columns it will have beforehand, I build the contents of this table in my pages Form_Load event handler.
That works well. But I'm populating that same table with textboxes, and I need to be able to read those textboxes if a submit button is clicked.
As it stands, it appears the table contents are cleared upon postback.
What is the best way to dynamically create textboxes in a table, and then read the values of those textboxes when the page is posted back?
If you want to persist the values on postback you will need to recreate the table control hierarchy on the Page_Init event. This usually means that you will need to rebind your table control to a data structure coming from the database.
Once the control hierarchy is in place, ASP.NET will load any data from the ViewState. This data can be in fact data that was previously set from the server on the textboxes. This step will ensure that these values are preserved on the control.
In the next step ASP.NET will process the POST data, and here basically will update the table textboxes with the input from the client (browser). Any values set from the ViewState will (most probably) be overwritten.
All this happens before Page_Load, so at this stage you should be able to access the client textboxes values.
Check out the ASP.NET Page Life Cycle, especially this image.

Automatic data binding mysteries

I have some questions about how and when data sources are bound in the page lifecycle, and I can't seem to find the answers anywhere.
I have a gridview which is bound to a data source at design time. One of the parameters for the DS is the selected value of a dropdown list. These dropdown lists are also databound to retreive their options, and the dropdowns have their auto-postback property set true. Now some things are confusing me about this.
Most of the time changing the value of the dropdown will reload the gridview with the new parameter, and it's not necessary to manually call databind() on the gridview in order to do so. However there are situations where the gridview is not rebound, and I'm not sure what these conditions are. When will a data-source be rebound automatically upon parameter change, and when do you need to call databind manually from code?
Does calling databind from codebehind prevent the automatic databind event from firing, or will they both fire, resulting in a wasteful extra query of the data source?
If the former, is there a way to stop the extra databind from occurring without having to move everything into code behind and lose the convenience of setting up data sources for the controls in the design view?
Are the answers to these questions documented on any official sources? (MSDN, etc)
I can't be sure without looking at you code, but you probably want to read about ViewState.
Once the control has been loaded, the data stays in the viewstate. You generally want to do the DataBinding on (!IsPostBack) event so that on postbacks you do not need to rebind the data from the DB.

databinding without FormView?

I have an ASP.NET page with a Wizard control containing several steps and about 80 form fields. The data is collected and inserted to a database from the code behind page. I need to set this form up so you can not only insert, but edit a record as well. So I want to databind the fields. I'd rather not use a FormView because then I would have to revise my existing code, since you can't access controls inside a FormView directly. Is there a way to databind the fields without using a FormView? I'm new at this by the way so sorry if the answer should be obvious.
A wizard is just a UI control with many steps in it. You can use it to insert, edit, delete or anything else you can think of. You can have an INSERT wizard and an EDIT wizard. The difference would be that there would be two of them and that the code behind for each one (presumably on the CompletedStep) would have slightly different code to persist the data. The insert wizard would call an insert database query and the edit one would call an update query.
That being said, you can access the control inside a FormView, I'm not sure why you said that you can't access controls indie a FormView. You can. See Using FindControl: Accessing Controls in a Formview.
You could even put the two wizards inside the two states of the formview - InsertTemplate and EditTemplate but thats getting a little crazy :)

ASP.NET, what is a datagrid bound to after postback?

I have a gridview full of telephone numbers. To populate the gridview I bind the gridview's datasource to a List<> of telephone numbers. I do this when the page is first loaded, but not on postbacks.
I want the user to be able to delete some of the telephone numbers, and then, if they want, click a Save button, and this will update the database, otherwise their changes will be ignored. So I have a button in the grid, and an event is fired, and I can call DeleteRow(row index) and remove the row from inside this event. For some reason this doesn't work.
All the gridview examples I find on the Internet execute the delete straight away by calling an sql function, and then bind again. And some examples bind the grid every time the page ios loaded, which seems inefficient.
My questions is:
The delete button causes a postback to the server. On postback the list of telephone numbers no longer exists. And the gridview's datasource is null. The grid is no longer bound. But there must be data somewhere, because the data in the grid is still visiable. Where is this data, and can I delete a row of it, so that a row in the gridview is deleted?
The viewstate saves the contents of the datagrid, so the answer is "The Viewstate"
Understanding the viewstate is essential to understanding how ASP.NET works, so rather than posting just enough info to answer your question, I'm going to recommend you read the entire article I linked to.
You can use jquery , you need to save the Datakey value of each deleted row in a hidden field
and hide the selected row , and when user clicks save , u can delete the rows based on hidden field values # code behind.

ASP.NET GridView problem

Well i have a gridview where i have defined the columns on my own and turned autogenerating off but now i have the problem that i cant access GridView.SelectedRow.DataItem.
As it turns out to be null now, when it had a value when auto generation was turned on..
Edit:
What i need is a way to save the ID of the row while not showing the ID to the user so if there is any way to do this?
I'm guessing DataItem is only properly filled when you are using DataBinding.
Are you using DataBinding?
Ok from this url:
The GridView (and actually, all our
data controls) does not save data
items across postbacks. This reduces
ViewState (if the objects are even
serializable) and enables garbage
collection to happen and clean up your
objects. So, when you click the
button to post back, the GridView has
not called DataBind and therefore your
data item isn't there. This is what
you've discovered.
Guessing you're reading the value from a postback, might just be the problem.
Try using SelectedValue, if you've setup the (primary) key for the items.
I've always used that and it worked.
msdn about SelectedValue
You can create a new hidden template column that will have a label with the ID . and in the cs file you use .FindControl on the rows.
You also have DataKeys property on the gridview, witch I think also does what you want

Resources