How to reference server side controls in JQuery ASP.NET?
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.
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)
$('#<%=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()");
精彩评论