views:

332

answers:

8

If I have a custom ASP.NET control that renders as an html control with an ID, I'm trying to add an attribute to the input control with jQuery.

However, I'm running into some issues:

First, my jQuery is not able to select it.

I have so far:

$(document).ready(function() {
  $(client id of the input control).attr('onclick', function () { alert('hey!'); });
});

It seems as if the jQuery is trying to find the input control but cannot.

Any ideas?

UPDATE:

Normally, the solutions presented would work but since this is a SharePoint Publishing .aspx page, Code blocks are not allowed... Trying other work arounds.

A: 

Is your ASP.NET control in a master page or a user control? ASP.NET will prepend a bunch of identifiers to your ID to make sure it is unique.

What is the ID of the control coming out as in HTML?

Andrew Barrett
I'm rendering the control first and viewing the html source for the ID and getting it there.
LB
+2  A: 

What do you use as the ID of the input control?

You have to use the ClientID of the control, not it's ID.

GoodEnough
Yes, but since this is client side, I don't have access to the ClientID. Therefore, for now, I've been just rendering the control and then hardcoding the ClientID.
LB
Yep, you have to work around that. You can use the ClientScriptManager to add a piece of javascript to run on startup (with the RegisterStartupScript method).
GoodEnough
A: 

If you're setting the ID of the rendered HTML control to the ID given to the ASP.NET control, you need to be aware of how ASP.NET handles control ID namespaces. Essentially, each control nested within a control needs to always guarantee a unique identifier. ASP.NET maintains this by taking the ID assigned to a control and prepending it with the ID of every control that contains the control. Thus, a control on a page will contain a string of ids going up to the form on the page itself.

You do not want to have to re-construct this client-side. Instead, Control defines ClientID, which will return the ID of the control as rendered on the client. Embed this into your jQuery, or into a javascript variable that your jQuery can see if you are putting the jQuery in a separate script file) and you should be fine.

John Christensen
+5  A: 

Just wire up the click event in jQuery.

$(document).ready(function() {
    $(id of the input control)click(function() {
        alert('hey!');
    });
});

Also, make sure you aren't just putting the ID you typed in to the control in the jQuery. ASP.Net renders the control with a much different ID than you had given the control. On one of the sites I run, I have the following button:

<asp:Button ID="btnSignup" runat="server" Text="Sign Up" />

The ID in the rendered HTML is not btnSignup, it actually renders as ctl00_cpLeft_btnSignup. To select that control with jQuery, you can do this:

$("input[id$='btnSignup'").click(function() { ... });

Edit:

$("input[id$='btnSignup'}").click(function() { ... });

You can also select using:

$("#<%= btnSignup.ClientID %>").click(function() { ... });

Edit:

I looked into using the <%= ... %> method and you would have to have your javascript IN the ASPX/ASCX file itself for it to work. Because of this, I'd stick with the regex method.

Gromer
Never thought about using the attribute matching selector. +1 for that instead of <%= Button1.ClientID %>
Chad Ruppert
I really want to run a comparison against the two methods to see which is faster.
Gromer
Gromer, still can't select it with the first method :($("input[id$='btnSignup'").click(function() { ... });
LB
Ugh, I missed a closing bracket. Check my edit.
Gromer
A: 

just to inform you, the document.ready part is not needed, using asp.net control the id is changed to something different then what is expect, a ct100.... is thrown on the beginning of it, so this way will look for the actual id name even though asp.net throws the mangled code on the beginning,because it looks for all elements (*) that have an ID that ends with the specified text.

$(function() {
   $("*[id$='idofcontrol']".click(function() { alert('hey!'); });
});

or go to the view source and copy the id name and paste it in your code or just use the css class selector as so:

$(function() {
      $('.myclassname').click(function() { alert('hey!'); });
});
TStamper
+2  A: 

You need to use its ClientID. If your ASP.NET markup looks like this:

<uc:MyControl runat="server" ID="myControl1" />

Your jQuery selector should look like this:

$(document).ready(function() {
  $("<%=myControl1.ClientID%>").attr('onclick', function () { alert('hey!'); });
});
Jose Basilio
A: 

Since you mentioned Sharepoint you might be interested in this series on EndUserSharepoint that focuses on integrating jQuery with Sharepoint. The author discusses getting jQuery to manipulate the webparts.

David Robbins