0

What is the recommended approach to reference ASP.NET server side controls in JQuery?Currently I use something a mix of server+client side

$('#<%=txttest.ClientID %>').focus();

I read somewhere it is not a good approach and it slows down things somewhat.

2 Answers 2

4

It does not slow things at all from the selector point of view. it will slow down things because this code will have to be inside the HTML and not inside the JS which can be compressed and minified.

As a .net developer in my past (not that far), I always prefered selectors to refer classes then ID's.

something like

$("input.textInputClassName").val('this is the new value');

because in .net you have no control over the ID's (ARGHHHHH) then you should select using classes (IMHO of course)

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

5 Comments

+1 for it will slow down things because this code will have to be inside the HTML and not inside the JS which can be compressed and minified.. Also - RE last sentence, this has changed in .NET 4 (you can specify static ID's, hence you could safely use $('#txttest')
@RPM1984 I know this feature in .net 4 and remember I was very happy they added it. thanks for the +1
Oh and also - you could use a server-side literal to include the code in an external JS file, in which case it could be compressed and minifised. But i guess im rambling now.
thanks Avi, would I be able to reference the elements with CSS selectors when I move my jscript to a separate file ? Also isn't $("input.textInputClassName") & $(".textInputClassName") the same thing ? Is there any reason to use the parent tag 'input' ?
@Popo, yes you will be able to refer to them from an external file, that is the point :-), about the selector, it's actually not the same. input.someClass will look only for inputs with this class and the other .someClass selector will look for all elements with this class name. if you know what you are looking for, it's alot "cheaper" to specify it in the selector.
0

$('#<%=txttest.ClientID %>').focus(); this code works only when you have written java Script in the same .aspx page but when your using separate javascript file for your page then this code will not work.

The approach what i prefer is if the controls are static then i will specify the 

name

Ex : $('#txttest').focus();

or if the controls were created dynamically then i will register the script or add the attributes to the page

Ex : BtnDailog.Attributes.add("onclick","$("#divMsg.show()");

1 Comment

I don't like doint "onclick" from the server side, it's not best practice IMHO. the first way you mentioned is way better but it's also a bit of a problem in .net because the ID's can change.

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.