realtek
realtek

Reputation: 841

OnClientClick = true does not fire server side code

I have an asp.net page which I would like to display a confirmation dialog before a server side event is triggered.

However when the user clicks the OK button, its does not fire the server side event.

I have tried everything that is mentioned in other questions that were posted.

I think the issue could be because I am also using an update panel:

Here is the code:

<asp:ScriptManager runat="server"></asp:ScriptManager>
    <asp:UpdatePanel runat="server" ID="myUpdatePanel" UpdateMode="Conditional">
         <ContentTemplate>
             <asp:Hyperlink runat="server" ID="InstanceHyperlink" Visible="false"></asp:Hyperlink><asp:Button runat="server" Visible="false" ID="DeleteInstance" OnClick="DeleteInstance_Click"  Text="Delete Instance"/>
                 <asp:Button runat="server" OnClientClick="if ( ! createInstanceConfirm()) { return false; }" OnClick="CreateInstance_Click1" ID="CreateInstance" UseSubmitBehavior="false" Text="Create Instance"></asp:Button>
        </ContentTemplate>
    <Triggers>
        <asp:AsyncPostBackTrigger ControlID="CreateInstance" EventName="Click"/>
    </Triggers>
</asp:UpdatePanel> 

Javascript:

<script type="text/javascript">
    function createInstanceConfirm() 
    {
       UIkit.modal.confirm("You are about to create a new instance", function ()
       { 
           return true; 
       });
    }
</script>

Upvotes: 1

Views: 845

Answers (3)

realtek
realtek

Reputation: 841

I found that this is using callbacks, I managed to get it working by calling another Javascript Function that manually did a post back on the button:

  function createInstanceConfirm() {
        return UIkit.modal.confirm("You are about to create a new instance for Development", function () {
            JSFunction();
        });
    }

     function JSFunction() {
             __doPostBack('<%= CreateInstance.ClientID  %>', '');
         }

It works the same way as JQuery confirm dialog.

Upvotes: 0

Tim Schmelter
Tim Schmelter

Reputation: 460208

You are returning false but if you don't want to cancel the postback you have to return true.

So instead of:

OnClientClick="if ( ! createInstanceConfirm()) { return false; }"

you could use this (assuming that the method returns either true or false):

OnClientClick="return createInstanceConfirm();"

I haven't used this component before but you could try this:

function createInstanceConfirm() 
{
   bool confirmed = UIkit.modal.confirm("You are about to create a new instance", function ()
   { 
       return true; 
   });
   return confirmed;
}

Upvotes: 6

bigrbuk
bigrbuk

Reputation: 56

Have you tried removing the EventName property from the ASyncPostBackTrigger - don't think that's needed. Check the page source once rendered and see what events are linked to the button in question.

Upvotes: 0

Related Questions