Reputation: 2124
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.
Upvotes: 11
Views: 24052
Reputation: 34
Never use eval() because of security leaks and vulnerability. Best way to do this is with using of native js click event like that.
$('[selector]')[0].click();
in your case
$("#myLink")[0].click();
Upvotes: 1
Reputation: 37785
Instead of
$("#myBtn").trigger("click");
Try this
eval($("#myBtn").attr('href'));
See this answer for more info: https://stackoverflow.com/a/943441/1207991
Upvotes: 23
Reputation: 103348
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");
Upvotes: 5
Reputation: 13250
You're giving same ID
to both of them which don't work.Try to change it.
If you're using master page then use as shown below:
<script type="text/javascript">
$(document).ready(function () {
$("#<%=myBtn.ClientID%>").click(function () {
$('#<%=hyperlinkID.ClientID%>').trigger('click');
});
</script>
If you're don't use master page then use the code below:
<script type="text/javascript">
$(document).ready(function () {
$("#myBtn").click(function () {
$('#hyperlinkID').trigger('click');
});
</script>
Upvotes: 1
Reputation: 13743
Use this line
$("#<%= myBtn.ClientID %>").trigger("click");
Upvotes: 0