Software-Entwicklung, Projektleitung, Web-Design
Kontakt:   +41 61 927 18 30
You are here:


Ajax cannot kill you, but sometimes it tries...

by Joerg Lang | Jan 07, 2011

Well it is really true. Using Ajax is cool and definitely a must when you design a new ASP.NET site. But sometimes it leads to programming phenomenons that are hard to track down.

Today I was working on a project where I have to use the standard components that come with ASP.NET and the AJAX toolkit. I had a GridView to update and delete a few values from a XML file. Using the built in commands from the GridView it was easy to add the update and delete features. However the GridView does not have a built in Insert feature. So I added a panel with the fields that need to be inserted, added some RequiredValidator controls and used the ModalPopupExtender from the AJAX toolkit to show the insert form, when the user clicks on a "Add new row" link above the grid.

Actually quite easy and also quite a nice interface for inserting new rows to the table.

But then the problem begun...

When I retested the form, I was able to add new rows, delete existing rows, but the update feature did not work anymore. The GridView switched to edit mode, but when clicking on the Update link nothing happened. The Cancel link however worked, but the RowUpdating event did not fire anymore. Nothing, zero, nada, zip, dead silence. I thought that must be a known problem and I will find a solution on the internet quickly. But no, nothing that really described my situation.

Have you found the problem, following the descriptions what I did? No? What if I told you that it is quite obvious?
Well the problem were the validation controls on the hidden AJAX panel. When the Update link tried to post back the changed values from the grid, the hidden validation controls on the hidden AJAX panel kicked in. The values for the two fields I had on the form were empty, so the postback never occurred.

So as you can see, AJAX (the great warrior) really tries to kill you sometimes.

The solution to the problem is quite easy. Either the grid should not cause validation on postback, or the hidden fields must not be empty. I opted for the second solution. When the GridView switches to edit view, I put some dummy values into the fields and after the update is finished I clear the fields again. After having implemented these few lines of code, the form was working again as expected.

Hope this description might help you in a similar situation when a event does not fire in an AJAX scenario.