3

This doesn't work and I don't understand why, I have tried that code on W3schools and that works, I think the problem might be the reference or something, I'm new to ASP.NET.

ASP Code(Master Page)

<script src="./Scripts/jquery-1.4.1.js" type="text/javascript"></script>
<script type="text/javascript">

    $(document).ready(function () {
        $("#bt_insertar").click(function () {
            alert("Handler for .click() called.");
        });
    });
</script>

<asp:ContentPlaceHolder id="head" runat="server"></asp:ContentPlaceHolder>

Index.aspx

<td class="style4">
    <asp:Button ID="bt_insertar" runat="server" Text="Insertar" Width="71px" 
    onclick="bt_insertar_Click" style="height: 26px" />
</td>
2
  • What is ClientIdMode set to? Have you inspected the html to ensure that you're actually getting the id generated that you think you're getting? Commented Jul 13, 2013 at 17:48
  • As an aside, I'd suggest you might want to try the free "Try jQuery" course from CodeSchool if you are new to jQuery and JavaScript as it gives an excellent introduction to the library which will help give you the confidence and skills to write jQuery code. codeschool.com/courses/try-jquery Commented Jul 14, 2013 at 17:55

3 Answers 3

5

By default, the ID you give your runat="server" control isn't the id that ends up on the DOM element. You can get the ID that ASP.Net generates from ClientID property on the server-side control object. E.g., change:

$("#bt_insertar").click(...

to

$("#<%= bt_insertar.ClientID%>").click(...

...if that code is in a page ASP.Net parses (as opposed to an external JavaScript file).

As of ASP.Net 4, you can control this behavior via the ClientIDMode property. For instance, if you use the Static mode (control.ClientIDMode = CLientIDMode.Static;), then the ID is in fact passed through as-is. But the default value for ClientIDMode is Predictable, which modifies the ID you use.

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

2 Comments

This property is only available in ASP.NET 4 and above for clarity. The purpose of the concatenated ID in ASP.NET is to prevent duplicate IDs when you are using template controls or repeating elements/user controls. Use static IDs with care within user controls or templates to avoid this duplication.
I am indeed - ClientID has been available since ASP.NET 1 to clarify the clarification.
1

To Bind jquery function to Asp.net Server control you have to write code this way.

<script type="text/javascript">

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


  });

Comments

0

Several points:

  1. In your JQuery include statement the src should be src="../Scripts/jquery-1.4.1.js" instead of src="./Scripts/jquery-1.4.1.js"
  2. You don't really need to bind click event in your document.ready event instead you can include OnClientClick="bt_insertar_Click();" in your asp:Button

and then you can define your function bt_insertar_Click()

    <script type="text/javascript">

    function bt_insertar_Click() {
       alert("Handler for .click() called.");
    }
    </script>

It is a better programming practice to use a separate .js file for you web page so you can move all the relevant js functions to this js file and then include it like you did with the jquery file.

Comments

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.