[RESOLVED]Calling asp:Buttons server side event from jQuery

Hi,

I’m trying to call a button’s OnClick event from jQuery. Here is a simplified version to demonstrate:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Test.aspx.cs" Inherits="TheBigIdea.FE.Test" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title></title>
        <script src="Scripts/jquery-1.10.2.js"></script>
        <script type="text/javascript">
            function Test() {                     
                $("#<%=Button2.ClientID%>").click();         
            }

            function Test2() {
                alert('Hello');
            }
        </script>
    </head>
    <body>
        <form id="form1" runat="server">
            <div>
                <asp:Button ID="Button1" OnClientClick="Test()" runat="server" Text="Button" />
                <asp:Button ID="Button2" OnClientClick="Test2()" OnClick="Button2_OnClick" runat="server" Text="Button" />
            </div>
        </form>
    </body>
</html>

This calls the client side method Test2(). But I want to fire the server side event OnClick. I only added Test2() as nothing was happening and I suspected that $("#<%=Button2.ClientID%>").click(); was trying to fire a client side event. I changed it to various
versions similar to: $("#<%=Button2.ClientID%>").onclick(); but nothing has worked yet.

Please help if you can.

Thanks,

Dave.

You need to hook up the server event. You need to specify the onclick handler. Currently the button has no handler specified. 

Hi,

Can you show how I specify the onclick handler please?

Like the onclick that you have in button2. Plus the actual handler in the code behind which button2 should have. So follow what you’ve done for button2 basically. 

Leave a Reply