Wednesday, 12 March 2014

RESOLVED: Button postback not firing from GridView or DataGrid

I am porting an older site to a newer site, replacing old code with new code, bootstrap 2 to bootstrap 3 etc and have moved a DataGrid into a new location but it needs to do the same thing. I had an edit and a delete button in a TemplateColumn and although it looked all fine and dandy, it just wouldn't post back. The generated .Net Javascript looked correct but nothing. I could get a Javascript confirmation but nothing on the server side.

I then noticed an answer in one of the hundreds of forum posts I tend to search when it doesn't work.

CausesValidation=False

This has caught me before. By default, postbacks will (correctly) call the validation functions on all validators on the page. This would normally be obvious since those validators would then complain about whatever you got wrong. In my case, I had a hidden form with loads of validators so, of course, nothing appeared to happen to me.

Setting CausesValidation=False to the link button in question means it passes a boolean false into the post back javascript function and this allows it to postback without validation! Amazing.

(Other potential problems)
1. If you have built your site from scratch, you might have forgotten to include the MS webforms Javascript files which include the relevant Javascript for the postback functionality.
2. Linkbuttons possibly won't always work since they no longer have a "name" attribute. You could add this manually during Page_Init if you wanted to.
Post a Comment