Jeremy
Jeremy

Reputation: 5435

Firing the OnClick function of an asp:Button from calling .click()

I'd like to call some server-side code from a Javascript function.

I have this button:

<asp:Button ID="quickAdd" runat="server" text = "Quick add" OnClick="QuickAdd" />

If I click it, the C# function QuickAdd is called as expected

I have this command in a javascript function:

document.getElementById("quickAdd").click();

and the execution of this function does nothing. No error, I assume it simply clicks the button but this doesn't cause trigger the event necessary for the QuickAdd C# function to fire.

How do I get around this?

Upvotes: 1

Views: 4400

Answers (5)

VoidA313
VoidA313

Reputation: 546

 $(document).ready(function () {

        $('#btnCancel').click(function (e) {
            e.preventDefault();

            $("<div><span><b>Are you sure you want to cancel this order?</b></span></div>").dialog({
                modal: true,
                draggable: false,
                resizable: false,
                width: 430,
                height: 150,
                buttons: {
                    "No": function () {
                        $(this).dialog("destroy");

                    },
                    "Yes": function () {
                        $("#btnCancel").unbind();
                        $(this).dialog("destroy");
                        document.getElementById('<%= btnCancel.ClientID %>').click();

                    }
                }
            });
        });

    });

Then in the Body

<asp:button id="btnCancel" runat="server" cssclass="button_major" text="Cancel" style="float: right"
                    onclick="btnCancel_ClickEvent" clientidmode="Static" />

Upvotes: 0

frosty
frosty

Reputation: 21762

the .click() is not native in Javascript (it is in jQuery). So, when you call .click(), nothing is going to happen, as .click() isn't specificed.

If you would like to get the onclick method for your object, use the following code:

 var func = document.getElementById("quickAdd").onclick;

At this point, func is the onclick function of your #quickAdd element. So, at this point, you can call:

func();

or

document.getElementById("quickAdd").onclick();

Either of those will get and execute your onclick event.

If this doesn't help, pull up your page in Firefox and execute your javascript with the Firebug console (Firebug is a Firefox plugin that you can install, and it gives you access to the ).

Upvotes: 0

Denis Biondic
Denis Biondic

Reputation: 8201

instead

document.getElementById("quickAdd").click();

use

document.getElementById('<%= quickAdd.ClientID %>').click();

where the quickAdd is actually the code name of your button variable. This is only because you will not be able to reference that html item because at runtime ID will change due to runat="server" - so this is only partial answer

Upvotes: 1

balexandre
balexandre

Reputation: 75073

you have 2 ways.

  • you can forget the QuickAdd server event and make an ajax call (see here how can this be done)
  • you can use the ASP.NET AJAX call to perform a Post, just hover the mouse in your link and see the javascript that will be executed, copy and use that method instead .click()

Upvotes: 0

jcolebrand
jcolebrand

Reputation: 16025

Specifically you should try the .onclick() method however a more thorough treatment of the matter is given here:

Is it possible to trigger a link's (or any element's) click event through JavaScript?

Upvotes: 0

Related Questions