0

When referring asp.net control inside a MasterPage in this way:

    $(function() { 
        $('#<%=txtMunicipio.ClientID%>').autocomplete({
            source: function(request, response) {
                $.ajax({
                    url: 'http://localhost/Autocomplete/WSAutocomplete.asmx/Poblacion',
                    data: 't=' + request.term,
                    type: 'POST',
                    //contentType: 'text/xml;charset=utf-8',
                    dataType: 'xml',
                    success: function(data) {
                        response(a = $.map(splitResponse(data), function(a, n) {
                            return formatCity(a, n);
                        }));
                    }
                });
            },
            ........

I am getting error "Object expected" because control does not exist in page. How can I check if control exists before associate autocomplete function?

Thanks in advance

2
  • Is the control defined in the master page or other pages? If the latter then I don't think this will work, the control MUST be in the master. Commented Jun 24, 2011 at 12:31
  • The control is defined on other pages, not in master page, but it should work as in other pages it is working. The problem is that this page is using a Wizard, so the control is not present in all steps. Commented Jun 24, 2011 at 12:52

2 Answers 2

1

First basic thing, make sure jquery and autocomplete js files are included.

If you have a master page the ID might get changed and even if you don´t, the safest way i find is if($(´[id$=txtMunicipio]´)!=undefined && $(´[id$=txtMunicipio]´).length > 1) { //do your thing } else { //handle the problem }

and your problem might just be that you need to wrap your code around a: $(document).ready(function(){ //your code goes here });

and in case of an update panel, if you wanna run a script on every load go for: function pageLoad(){ //your code here }

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

1 Comment

Ok, I was including jquery files incorrectly. So when included correctly I can use (as said by sapan): if($('#<%=txtMunicipio.ClientID%>') > 0){...
1

$("#mydiv").length > 0

http://aaronrussell.co.uk/legacy/check-if-an-element-exists-using-jquery/

3 Comments

I am new to jQuery. Shoul I do: '$function(){ if($('#<%=txtMunicipio.ClientID%>').length) { $('#<%=txtMunicipio.ClientID%>').autocomplete({.... I am not sure where to put the check expression.
Alfonso, You should check your .NET code first it is very unlikely that the control is not getting created because $(function() { }); only runs after all the elements are added to the DOM. Also it is not $function()... it is $(function() { // Handler for .ready() called. }); How are you creating this element in .NET? Also have a look at api.jquery.com/ready
Thanks sapan, the problem was I was not referencing jQuery libraries fine from my masterpage. Now the problem is solved.

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.