Alexandre
Alexandre

Reputation: 13308

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>

Upvotes: 3

Views: 2008

Answers (4)

Sushanth --
Sushanth --

Reputation: 55750

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

Upvotes: 0

VMAtm
VMAtm

Reputation: 28355

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" /> 

Upvotes: 3

Amir Ismail
Amir Ismail

Reputation: 3883

try this

$('#<%=LinkButtonObj.ClientID%>').click(function() {
 alert('Handler for .click() called.');
});

Upvotes: 3

IUnknown
IUnknown

Reputation: 22448

$(function () {
    $('#<%= LinkButtonFirst.ClientID %>').bind('click', 
    function () {
        alert('fsfsf'); 
    });
});

Upvotes: 3

Related Questions