tags:

views:

39

answers:

3

Hi guys,

I use a lot of JS in my ASP.Net page. At the moment I try to access a lot of labels in usercontrols. In normal labels without UCs, $get and getElementByID both work. But now not.

        alert(document.getElementById('<%= ucBW1.FindControl("lblTime").ClientID %>').innerHTML);
      alert($get('<%= ucBW1.FindControl("lblTime").ClientID %>').innerHTML);

So, the usercontrol-control-id (what a word) is found correctly by both, but the $get gets an "object expected". (Of curse I tried it seperatly).

Any ideas?

(The alert is only to test what I get, I want to change the innerHTML)

+1  A: 

Try this :

alert($('#<%= ucBW1.FindControl("lblTime").ClientID %>').html());

In JQuery, '#' means that you're trying to get element by it's id.

Jimmeh is right, I misread that part. The get method is used to return actual DOM elements that matched, here is a usage of get function in your case :

alert($('#<%= ucBW1.FindControl("lblTime").ClientID %>').get(0).innerHTML);

Also, you can use html function to set item's innerHTML like that :

$('#<%= ucBW1.FindControl("lblTime").ClientID %>').html('<b>some stuff here</b>');
Canavar
Thank you, works. 1) Why? 2) What was my problem? 3) Sadly your HTML() I can't assign a text, which I must, see edit please.
Kovu
Wunderful, +1 and ansered.
Kovu
$get and $.get are a little different.
Jimmeh
@Jimmeh : thanks, I misread that part, fixed now.
Canavar
+2  A: 

If you're using jquery, use

alert($('#<%= ucBW1.FindControl("lblTime").ClientID %>').html());
Jimmeh
Thank you, works. 1) Why? 2) What was my problem? 3) Sadly your HTML() I can't assign a text, which I must, see edit please.
Kovu
You can assign text to html() by doing html('text here'). It will replace whatever is there. jquery requires the # for element Id's, which is why it didn't work i think.
Jimmeh
+1 for same answer
Kovu
A: 

If it expects an object, you can't feed it with a string (the ID is a string). Doesn't ucBW1.FindControl("lblTime") return the object? If so, you don't need to append .ClientID to get its ID.

Álvaro G. Vicario
jquery and asp.net controls aren't the most compatible things in the world. Although i've not tried what you've suggested, I would bet money on it not working.
Jimmeh