views:

28

answers:

2

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?

+1  A: 

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.

David Stratton
Thanks! That link was a good read. I have checked that there is no binding on postback, and saveviewstate is called AFTER the deleterow is called. I have noticed that when I call DeleteRow the number of rows in the grid is the same before and after. Should I be doing something in one of the deleting events?
Terry Stone
A: 

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.

arlen
I'd strongly suggest to avoid JQuery in this instance. Not to say that jQuery couldnt do it, but datagrid's have been in existance since the .net 1.1 days, and all postback data is stored in a ViewState (unless the ViewState's been turned off for the control of course). That being said.. jQuery cannot access ViewState data.
ewitkows
For this example and all the examples that have the same scenario, It's better to use client side resources , cause user not sure about removing completely the rows or not. After user clicks the save button , now you can do the removal process at the code behind , so I suggest JQuery,hidden field to implement the scenario.
arlen