views:

744

answers:

2

Hello all

I have honestly tried several different avenues from google on this erro but I am hitting my head against a brick wall.

I have this bit of jquery in my code behind:

tbxProdAC.Attributes.Add("onclick", "$('radProdAC.ClientID').attr('checked', true); $('ddlBuyer.ClientID').val('--Choose Buyer--'); $('ddlSub.ClientID').val('--Choose Sub Category--'); $('ddlProd.ClientID').val('--Choose Product--');");

However, whenever I click on the textbox, with debugging switched on, I received the above error on the following line:

 <input name="ctl00$ContentPlaceHolder1$tbxProdAC" type="text" onchange="javascript:setTimeout('__doPostBack(\'ctl00$ContentPlaceHolder1$tbxProdAC\',\'\')', 0)" onkeypress="if (WebForm_TextBoxKeyHandler(event) == false) return false;" id="ctl00_ContentPlaceHolder1_tbxProdAC" class="completionList2" onclick="$('radProdAC.ClientID').attr('checked', true); $('ddlBuyer.ClientID').val('--Choose Buyer--'); $('ddlSub.ClientID').val('--Choose Sub Category--'); $('ddlProd.ClientID').val('--Choose Product--');" style="z-index: 1; left: 200px; top: 475px; position: absolute; height: 20px; width: 345px;" />   

Now ctl00$ContentPlaceHolder1$tbxProdAC doesn't really 'exist. This is a control that is on a content page within a content place holder.

How do I go about ensuring that the jquery reconciles with the correct ID or how do I ensure that the content place holder does not amend the original control ID?

Any help greatly received as I am going mad trying different things!

A: 

In the onchange event handler you don't need the javascript bit you can call the function directly you only need to do this is you a calling the function by an href. You also cannot pass parameters the way you are doing in the timeout.

Might be worth calling the timeout in the function rather than as you call the event.

Either way you can call the function in the timeout anomously eg;

setTimeout(function(){youfunction here with parameters}, 0);

Also I would move all the things that are happening in the other events click etc into their own functions. If you move all this stuff into functions perhaps into an external js file you will be able to trace more what is going on as it will be clearer. Also use firebug for debugging if you are not already. If you need to get generated vars from the page then just about the vars in the page and call them from the js.

It is possible in jquery do to be too specific and traverse the dom to find stuff rather than refer to it explicitly not only does it make the code easier it makes it more generic therefore more reusable.

matpol
Hello Mat - I think I might just do it all server side i.e. on the onchange event handler as you suggest! At least I semi- know what I am doing there!!!
Ricardo Deano
A: 
tbxProdAC.Attributes.Add(    
    "onclick", 
    "$('" + radProdAC.ClientID + "').attr('checked', true); $('" + ddlBuyer.ClientID + "').val('--Choose Buyer--'); $('" + ddlSub.ClientID + "').val('--Choose Sub Category--'); $('" + ddlProd.ClientID + "').val('--Choose Product--');"
);

You need to get the client id, which has to be done outside of quotes.

"$('radProdAC.ClientID').attr('checked', true); needs to be changed to "$('" + radProdAC.ClientID + "').attr('checked', true); everywhere it is typed.

NickLarsen
Hello Nick - I have tried using the syntax you have suggested but the errors remains. Most frustrating.
Ricardo Deano
Can you show us the HTML rendered to the browser after making this change? It might help further with the problem.
NickLarsen