0

I'm having problems using JQuery inside an ASP.Net User Control I've created a sample and here is the markup for my user control:

<%@ Control Language="C#" ClassName="UC" AutoEventWireup="true" 
    CodeFile="UC.ascx.cs" Inherits="UserControls_UC" %>

<span id="Licenses"></span>

<script type="text/javascript">
    $(document).ready(function() {
        var ctlID = $("span[id$='Licenses']");
        ctlID.text = "Testing";
    });
</script>

If I include this script tag <script type="text/javascript" src="js/jquery-1.3.2.js" /> in the aspx file containing the user control to reference JQuery, nothing happens. If I don't include it, I get a JavaScript error dialog saying there was a runtime error and that an Object was expected. Any ideas what I may be doing wrong?

4
  • What happens if you try to call $("span[id$='Licenses']") from the FireBug console? Does it work? Commented Sep 16, 2009 at 18:27
  • Is js/jquery-1.3.2.js the correct path relative to the containing aspx page? Fiddler it, or better Firebug it. Commented Sep 16, 2009 at 18:31
  • I've included the reference to js/jquery-1.3.2.js in a <script> tag in the head section of the containing aspx file - is this not the correct place for it? It appears that may be causing problems. If I remark that out, then I get the javascript error dialog, if I un-remark it then nothing happens. Commented Sep 16, 2009 at 18:35
  • Are there any javascript errors on the page when the script reference is there? Commented Sep 16, 2009 at 18:52

4 Answers 4

1

text is a function in jQuery. Try:

 ctlID.text("Testing");
Sign up to request clarification or add additional context in comments.

1 Comment

This works either as a function call or assignment as long as I run it directly from the aspx page. Neither method works when run from the user control.
0

Prefixing the '$' to the id gets all elements that ends with "Licenses" .

Make a quick change to test this to '#' will get you exactly one element.

  $(document).ready(function() {
        var ctlID = $("#Licenses");
        ctlID.text = "Testing";
    });

To make it work with attribute selector try

$('span[@id=Licenses]')   // You can omit the @ in the latest version of jquery.

Comments

0

I think you should use something like this:

var ctlID = $("span[id$='Licenses']").get(0);
ctlID.text = "Testing";

Comments

0

Have you included the jquery library correctly on master page?

Is that not meant to be dollar before id, i.e. $id='Licenses' ? or even a #

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.