jQuery - attach event handler for linkButton
I have a user control. It contrains LinkButton. I want to attach an click event to the LinkButton using jquery.
<%@ Control AutoEventWireup="true" CodeBehind="Pager.ascx.cs" EnableViewState="true" Inherits="Web.Controls.Pager" Language="C#" ViewStateMode="Enabled" %>
<script type="text/javascript">
$(function () {
eval($('#LinkButtonFirst').attr('href')).bind('click',
function () {
alert('fsfsf');
});
});
</script>开发者_高级运维
<asp:LinkButton ID="LinkButtonFirst" runat="server" OnClick="LinkButtonFirst_Click" />
The first error I've encountered is Cannot call method 'bind' of undefined
.
<script src="/Scripts/jquery/jquery-1.5.1.min.js" type="text/javascript"></script>
try this
$('#<%=LinkButtonObj.ClientID%>').click(function() {
alert('Handler for .click() called.');
});
$(function () {
$('#<%= LinkButtonFirst.ClientID %>').bind('click',
function () {
alert('fsfsf');
});
});
Use the ClientID
Property of the LinkButton
.
IDs of the controls in user controls are computed not as ODs of the controls on page.
So you should use the code:
<%@ Control AutoEventWireup="true" CodeBehind="Pager.ascx.cs" EnableViewState="true" Inherits="Web.Controls.Pager" Language="C#" ViewStateMode="Enabled" %>
<script type="text/javascript">
$(function () {
eval($('#<%= LinkButtonFirst.ClientID %>').attr('href')).bind('click',
function () {
alert('fsfsf');
});
});
</script>
<asp:LinkButton ID="LinkButtonFirst" runat="server" OnClick="LinkButtonFirst_Click" />
As of jQuery 1.8.0 .live
and .bind
are deprecated. try using .on
to attach any event.
This makes sure the event is attached when the element is available
$(function () {
$('[id*=LinkButtonFirst]').on('click',function () {
alert(' Link Button was Clicked !!');
});
});
精彩评论