Tuesday 22 December 2009

LinkButton in SharePoint - Not firing Click Event

I had a sitution to stream a file from SQL Server.
ASP.Net LinkButton was used in the ITemplate to use in GridView and Click event was defined with statements like

response.AddHeader("Content-Disposition", "attachment; filename=" + fileName );
response.AddHeader("Content-Length", blob.Length.ToString());
response.ContentType = "Application/x-msexcel";
response.BinaryWrite(blob);



Everything was working perfect in WebApplication. When the webpart was deployed to SharePoint, the server side click event fired only once. After that the link became irresponsive. If the page is refreshed, the link worked once again for 1st time.

After several googling, found that SharePoint stores time-stamp on client side for security reasons and avoids multiple post-backs for the same control.

There are 2 ways to over-come this issue. Just add an appropriate line as below

1. Generic way

imageButton.OnClientClick = "this.form.onsubmit = function() {return true;}";
or
hyperLink.OnClientClick = "this.form.onsubmit = function() {return true;}";
or
linkButton.OnClientClick = "document.getElementsByTagName(\'form\')[0].onsubmit = function() {return true;}";



2.Specific to SharePoint

linkButton.OnClientClick = "_spFormOnSubmitCalled = false;_spSuppressFormOnSubmitWrapper=true;";

After adding single line (one of the above), the webpart worked like a charm.