views:

634

answers:

4

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?

A: 

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 #

dove
+1  A: 

text is a function in jQuery. Try:

 ctlID.text("Testing");
womp
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.
Russ Clark
A: 

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.
rajesh pillai
A: 

I think you should use something like this:

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

saeed