[RESOLVED]jQuery binding function to Gridview Select

Hi,

I have a gridview with Select LinkButton, static ID.

I try to add onclick method to the LinkButton (actually an a tag) with the following code:

$(function () {
            $("#LinkButton1").click(function (e) { doSomething(); });
        }
);

Unfortunately the the onclick event is not added to the Linkbutton,

ant idea why ?


$("#LinkButton1").click(function(){
  //doSomething();
});

Ashim’s solution will resolve your click event not executing issue.How ever you may face one more issue here since you have made the clientid mode static the javascript function will work only for the first button.
To Resolve it you need to remove the clientID mode property from linkbutton and use the Jquery’s class selector to access the control from gridview in jquery code

Sample Code
Assign a css class to your link button like given below

<asp:TemplateField HeaderText="Department_Name">
                <ItemTemplate>
                    <asp:LinkButton ID="LinkButton1" runat="server" class="lnkbutton">LinkButton</asp:LinkButton>
                </ItemTemplate>
            </asp:TemplateField>

Jquery

<script src="http://code.jquery.com/jquery-1.11.0.min.js"></script>
    <script>

        $(document).ready(function () {
            //Attach click event handler to your linkbutton using the css selector
            $(".lnkbutton").click(function () {
                doSomething();
            });
        });

        function doSomething() {
            alert('linkbuttonclick');
        }
    </script>

and then use the below code

Leave a Reply