0

I have an ASP Server Button on a User Control that triggers a Server-Side OnClick event which works fine. But I need to trigger the OnClick event using JavaScript or JQuery.

I have tried the following methods but neither actually simulate the results as if a User were to actually click the button:

document.getElementById('<%=btnRefresh.ClientID%>').click();
$('#<%=btnRefresh.ClientID %>').click();

I suspected this was possibly because this emulates a OnClientClick event and not the Server; but I have found claims that this has worked for others in successfully triggering the Server Side OnClick event but this is not working for me.

Is there any other way to accomplish the task?


In response to the suggestion of AJAX. I have tried that approach but a PostBack is required for this task to successfully complete. The actual Click of the button produces the desired result, I can cause the event to trigger using AJAX but without the element of PostBack it fails.

9
  • You need to use AJAX if you want to trigger a server event via the client Commented Sep 24, 2014 at 18:36
  • 1
    Do you have a client side event handler for that button? Perhaps it's returning false or otherwise preventing the postback from occurring. Commented Sep 24, 2014 at 18:37
  • 1
    @tymeJV No, you can cause a postback from client side code. It would be better design to use AJAX, but it's not necessary. Commented Sep 24, 2014 at 18:37
  • 1
    @mason -- True - should've mentioned postbacks...I just assume now-a-days no one wants a postback :D Commented Sep 24, 2014 at 18:38
  • 1
    @tymeJV Down with WebForms. :) Commented Sep 24, 2014 at 18:39

1 Answer 1

1

I have an ASP Server Button

so you have something like

<asp:Button ID="btnRefresh" Text="I'm a button" OnClick="btnRefresh_Click" />

In order to also execute a client call, prior to the server call, all you need is to attach a function or inline code to the OnClientClick property:

<asp:Button ID="btnRefresh" 
          Text="I'm a button" 
       OnClick="btnRefresh_Click"
 OnClientClick="btnRefreshClick" />

and in your javascript simple add that method

function btnRefreshClick() {

    // your stuff goes here...

    return true; // returning true (or nothing) will fire the server side click
                 // returning false will not fire it
}

Added: Misunderstood the question, so you want to fire a javascript event that will do the click on the button automatically...

I just tested this code:

<body>
    <form id="form1" runat="server">
    <div>
        <asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" /><br />
        <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
        <br/>
        <a href="#" class="a">fire button click event</a>
    </div>
    </form>

    <script>
        $(".a").click(function() {
            //var elm = $("#" + "<%= Button1.ClientID %>"); elm.click(); // with jQuery

            var elm = document.getElementById('<%=Button1.ClientID%>');
            elm.click();
        });
    </script>
</body>

and I get the button to fire correctly, with or without jQuery:

screencast of this being executed: http://screencast.com/t/m4ohA9uW

Sign up to request clarification or add additional context in comments.

9 Comments

The question wasn't about how to invoke client side code before server side. It was about how to simulate clicking the button from client side in order to cause a postback. This could be done from a different method, example a timer that refreshes the date every 5 minutes.
There's a Javascript call in the .NET framework called WebForm_DoPostBackWithOptions - I've hooked into this to trigger events using jQuery. I used FireBug to look at the signature and match what I wanted to do in order to get it to work.
Thanks Balexandre but that code looks eerily similar to what I am already doing although I viewed the screencast and clearly can see that its working in your example. The only thing that I see which is different is that My code resides inside a UserControl; There is no Form present. Could that make a difference???
I will to attempt implement your code precisely as you have it within my UserControl and will update with the result afterward. Thanks
in Webforms, you always need to have a <form> in each page and then your user controller lives inside that form... BTW, you want to access the click event from within your user controller, or from the final page itself where the user controller is placed?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.