5

I have a server side button as

<asp:Button ID="btnSummary" runat="server" OnClick="btnSummary_Click" Text="Next" />

I want to attach the jQuery Click event using its ID and NOT using the alternative class attribute way.

I tried to attach the click event as:

$("#btnSummary").click(function() 
        {
            alert("1");
        });

But, its click event is not fired. Also, I have also tried $("id[$btnSummary]").

Is there any way to attach the click event on asp:button using jQuery without the class attribute on the button?

1
  • add event.preventDefault(), either to the beginning or before any AJAX calls, if needed. Commented Feb 6, 2017 at 18:22

13 Answers 13

15

Some of the options you have can be found here

How to stop ASP.NET from changing ids in order to use jQuery

EDIT:

After reading your comments on other answers, it sounds like you need to bind the onclick event handler inside of ASP.NET AJAX's pageLoad, so that it is bound on every postback, including asynchronous postbacks. On the other hand, $(document).ready() binds only once on initial page load

function pageLoad(sender, args)
{
     $("#<%=btnSummary.ClientID %>").click(function() 
    {
        alert("1");
    });
}

Dave Ward wrote a nice article on the difference between pageLoad and $(document).ready().

Sign up to request clarification or add additional context in comments.

Comments

11

Add ClientIDMode="Static" to your asp:button, something like this:

<asp:Button ID="Button1" runat="server" ClientIDMode="Static" Text="Button" />

This will make the ID remain the same. It disables the autogenerated names for this control.

Here is the reference for this: http://msdn.microsoft.com/en-us/library/system.web.ui.control.clientid.aspx

Comments

5
  1. Check if the id attribute is in the html source when you run the site.
  2. Do you have the click function inside a document ready function ?

-

$(document).ready(function() {
    // put all your jQuery goodness in here.
});

EDIT:

Since its a server side control and the ID changes, you will need to dynamically update the javascript on every page load with the variable.

1 Comment

Yes.. My code is inside the document.ready portion. Id is also their in the HTML but its server side control. its ID gets changed. That's why I am asking this question.
1

ASP.NET generates a UniqueID for client-side stuff, you can use that to bind the event. That ID is generated based on the position of that Control inside a ControlCollection and different INamingContainers, it's ugly and you can't guess it...

Add this kind of code somewhere on your page to hook up that button.

<script type="text/javascript">
  $(function() { $("#<%=btnSummary.ClientID%>") }).click(function(){/*...*/});
</script>

5 Comments

I cannot use your suggestion as I have to add UpdatePanel dynamically. It's our website requirement.
Is the button inside of an UpdatePanel that you are adding dynamically?
Well, if you don't know the ClientID and you don't have a way of hooking the UpdatePanel onchange, you are, as we say, screwed. Seriously, you'll need to pass those ClientIDs as they would appear in HTML. If you use Firefox you can examine the returned DOM by the UpdatePanel.
The ClientID s are there, you'll just have to hook the binding on click events to the success of an UpdatePanel update. That should be easy.
you cant turn off the auto generated IDs
1

I'm little confused here now. Let me explain:

1. I added a button on the page:
<form id="form1" runat="server">
<div>
<asp:Button ID="Button1" runat="server"  Text="Button" />
<div>

2. Then I added a JavaScript and jQuery:
<script type="text/javascript">
                    $(document).ready(function() {
                    $("#Button1").click(function() {
                        alert("Hello world!");
                    });

                    });
                </script>

3. The generated html is this:
<div>
  <input type="submit" name="Button1" value="Button" id="Button1" />
<div>

Now, I don't see ASP.NET (asp.net 3.5) changing the ids. Why do I see different behavior?

Btw. This does work when I hit the button!

Thanks.

Comments

1

Assigning the selector to the class worked for me.

<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="My Button" CssClass="myButton"/>

<script>
    jQuery(".myButton").click(function () {
        alert("clicked");
    });
</script>

Comments

0

use pageLoad when using updatepanels because document.ready only runs once on initialization of the page and loses its binding on partial postbacks. PageLoad gets run everytime so will rebind every partial postback.

Comments

0

I had the same problem, this works for me

<asp:Button ID="Button1" runat="server" Text="ASP Button" OnClientClick="return false;" />

Its the addition of return false that seems to make the difference. Hope this helps.

regards

Peter

Comments

0

ASP.NET adds to you id (example: id "selectButton" becomes

"ctl00_middleContent_gvPeople_ctl04_selectButton");

Use the jquery syntax to search for the part of the id that doesn't change.

$("[id='_selectButton']")

Comments

0

Please use the following syntax : $("[id*=Button1]") to reference any asp control

Comments

0

Please try below:

document.getElementById("<%=button1.ClientID %>").click();

Comments

0

Wrap this <asp:Button/> inside a html <div> container.

<div id="testG">  
   <asp:Button ID="btn1" runat="server" Text="TestButton" />
</div>
$(document).ready(function () {
    $("#testG").click(function () {
                alert("1");
                //your code 
     });
});

Comments

-1

You have reached there. There are two ways i guess,

  1. Either inspect the control from Browser and See the Control ID, looks like ct1000_ btnSummary. use this ID on Click event of jQuery.

replace code

 $("#ctl00_contentplcedholder_btnSummary").click(function() 
    {
        alert("1");
    });
  1. Next one is quite easy just add an attribute ClientIdMode="static" to your control and use your actual Jquery click event it will fire.

2 Comments

very bad form....if the control is moved to a different container, then the id is changed. This does not allow for dynamic movement and does not solve the potential MVC or MVVM templating design.
@GoldBishop yea that's why better to choose the second option

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.