Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery: trigger <asp:LinkButton> click

i need to trigger the click of an <asp:LinkButton>.

Example1

The following thing is working:

<asp:Button ID="myBtn" runat="server" />

$("#myBtn").trigger("click");

Example2

Now the same thing with the LinkButton is not working:

<asp:LinkButton ID="myBtn" runat="server" />

$("#myBtn").trigger("click");

I need to trigger the click event of an asp:LinkButton.

like image 781
Keith L. Avatar asked Apr 16 '12 10:04

Keith L.


2 Answers

Instead of

$("#myBtn").trigger("click");

Try this

eval($("#myBtn").attr('href'));

See this answer for more info: https://stackoverflow.com/a/943441/1207991

like image 55
Gloopy Avatar answered Sep 27 '22 17:09

Gloopy


Add a CssClass property to your LinkButton and use that as your selector.

Other answers have suggested using ASP.NET ClientID to get the rendered ID of the LinkButton, which is fine, but this means you have to have your script inline in the page, and the script will only work for this 1 button.

Using a class means you can have multiple elements triggering the same event, and it means you don't need to use inline server-side code to get it to work.

<asp:LinkButton ID="myBtn" runat="server" CssClass="myLink" />

$(".myLink").trigger("click");
like image 40
Curtis Avatar answered Sep 27 '22 19:09

Curtis