0

I have an asp.net button in the gridview footer, and I wanna to call the server-side ASP.NET button event in the client side (JavaScript).

How to do this please code details if possible.

My .aspx:

<FooterTemplate>
    <asp:ImageButton ID="ibtn_add" ImageUrl="~/Images/accord_plus.png" 
         runat="server" OnClick="ibtn_add_Click" />
</FooterTemplate>

I wanna to know how to call the ibtn_add_Click in the client side.

in the following code:

var isShift = false;
document.onkeyup = function(e)
{ if (e.which == 16) isShift = false; }
document.onkeydown = function(e) {
    if (e.which == 16) isShift = true;
    if (e.which == 13 && isShift == true) {
        //Here I wanna to call ibtn_add_Click
        return false;
    }
}

Note :

The gridview in an update panel, and I execute the JavaScript through:

<asp:ScriptManager ID="ScriptManager1" runat="server">
    <Scripts>
        <asp:ScriptReference Path="~/Scripts/Shortcut.js" />
    </Scripts>
</asp:ScriptManager>
5
  • Where is the problem?? If you want to call function ibtn_add_Click, then call it ibtn_add_Click() Oct 2, 2011 at 9:05
  • Or you want to trigger onclick server handler? Oct 2, 2011 at 9:06
  • 1
    I think, it is not good practice to call server handler OnClick if click was not really performed. Think about sending PostBack or Ajax call. Oct 2, 2011 at 9:09
  • please more clarification ,the gridview in an update panel and i do the following: Oct 2, 2011 at 9:11
  • <asp:ScriptManager ID="ScriptManager1" runat="server"> <Scripts> <asp:ScriptReference Path="~/Scripts/Shortcut.js" /> </Scripts> </asp:ScriptManager> Oct 2, 2011 at 9:11

4 Answers 4

1

Use OnClientClick button's attribute.

OnClientClick="ibtn_add_Click()"

MSDN

2
  • hmmm i wanna to call it under specific condition in my javascript code Oct 2, 2011 at 9:02
  • explain, please your "specific condition" Oct 2, 2011 at 9:03
1

I think you want something like this ...

if (e.which == 13 && isShift == true) 
{ 
    __doPostBack('ibtn_add','OnClick');
    return false; 
} 
1

You might use ajax and make a call to webservice or web method or you can use or you can use

 __doPostBack(‘btntest','OnClick')

or

 $('btntest').trigger('click');

chck this link : http://codethatworkedforme.blogspot.com/2011/08/some-tricks-for-asynchronous-postback.html

1
 <asp:ImageButton ID="ibtn_add" ImageUrl="~/Images/accord_plus.png" runat="server" OnClick="ibtn_add_Click" />

This is the problem I have faced but I used only for OnClientClick

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.