DataBinding in ASP.NET 2.0 and the RowUpdating event
For a while now I’m trying to figure out why my method, triggered by the GridView.RowUpdating event, doesn’t work as all samples say it should do. All samples of course assume you’re doing everything in your .aspx page, but I have to do everything in my code-behind, because on forehand I don’t know what I’ll be binding to my GridView. Check out what I’m trying to do, maybe you can help?
First, I load up some data:
After that, I’m doing some work to make my GridView look beautifull, adding all columns by hand. But for this demo, I’ll show you the simple version, that also doesn’t work 🙂
Let’s bind the data to our GridView and have it autogenerate our columns, as well as an edit button.
Now I’ve tried a lot of places to setup subscription to the events. Currently I’m in the OnInit method, but it doesn’t matter where you place this code.
So now everything’s setup, let’s create a method for the RowEditing event.
Been there, done that… Okay, now the part that doesn’t work!
When we edit a row in our GridView and press the “Update” button, at some time it’s received in the RowUpdating method. But as I noted in the comments in that method, some collections that should contain the columns (names, old values and new values) are always empty. Always. And the RowUpdated method is never even touched!!!
Okay, so I’ve read on the ASP.NET Forums that I need to use a DataSource control. For example a SqlDataSource, which is automatically added to your WebForm if you drag-n-drop your way around Visual Studio 2005.The problem is, I’d very much like to do so, if ASP.NET 2.0 requires this. But I can’t set a DataSource property or anything on the SqlDataSource!!!
So if you have any solution to my problem… I probably have to read the cells on the GridView of the selected row, find the controls, get the values from those and insert those into my DataSet. Which means I won’t make my 70% code reduction Microsoft has always promised me. Bah!