Search This Blog

Tuesday 29 May 2012

ImageButton/LinkButton in SharePoint - Not firing Click Event Second Time

I had situation, where i need to export an application page to msword.

I used asp.net image button in update panel, 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 irresponsible. If the page is refreshed, the link worked once again for 1st time.

After several goggling, 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;";

I have gone with second one and resolved the issue. The following changes i have done in my webpart solution.

Added the below script

<script type="text/javascript">

    _spOriginalFormAction = document.forms[0].action;
    _spSuppressFormOnSubmitWrapper = true;       
   
</script>

Under the update panel i have added the below code
<asp:UpdatePanel ID="upManageJobAides" runat="server">
    <Triggers>
        <asp:PostBackTrigger ControlID="imgBtnExport" />
    </Triggers>

where the imgBtnExport is the asp.net image button id for Export to Word process.

No comments:

Post a Comment