开发者

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?


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


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?


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()


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 ).


 $(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" />
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜