开发者

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.

But the Master Page already have link to jquery

<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 !!'); 
    });
});
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜