Home » ASP.net

When explicitly binding asp:ListView the first item’s EditItem == null in the ItemUpdating event

27. July 2009 by Dave Hawes 0 Comments

I have just had one of those weeks where nothing seems to be simple, now finally after countless hours of head scratching I think I have a solution to a problem that has dogged me for over a month now.

I am using an asp:ListView on the Online File Store of my http://www.skillbook.co.uk website (check it out, it’s free to sign up to!!). I decided to explicitly bind my data to the control rather than use the <%# Bind(“”) %> syntax for reasons that are not important. During testing we had reports that when saving an edit of the first item in a page an exception was being thrown the good old ‘object reference is not equal to an object’.

Back on the Dev environment I could not reproduce it. On the internal server I could not reproduce it. On the live server it was falling over like my 5 week old baby trying to stand when I clicked save.

I have found other people with this problem:

http://forums.asp.net/p/1275744/2424258.aspx#2424258

http://www.ureader.com/msg/14254035.aspx

but no solution. With the pressure on to fix this in the live environment I have finally found a solution that seems to work, and I’m afraid that it is phugly code.

With the listView.EditItem == null the answer for me was just to get the item out of the ListView using the index that is sent through as part of the ListViewUpdateEventArgs.

So instead of:

                 ASPxTextBox txtName = (ASPxTextBox)listView.EditItem.FindControl("txtName");

It becomes:

                 ASPxTextBox txtName = (ASPxTextBox)listView.Items[e.ItemIndex].FindControl("txtName");

 

I really hope this helps some people out there!

Comments are closed