Click here to Skip to main content
15,881,588 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
For some reason I want to implement code like


JavaScript
<script language="javascript" type="text/javascript">
    var value = <% TextBox1.Text %>;
   </script>


but its not working, I know one way to do it is like

JavaScript
var value = $('#<%= TextBox1.ClientID %>').val();


but I just want to know why the first one always return an empty value, any idea?
Posted
Updated 5-Jun-13 11:45am
v2
Comments
Richard C Bishop 5-Jun-13 17:20pm    
The "<% %>" symbols signify classic ASP. You would need to use VBScript between those tags, not C# and that is provided you can even do that(I am not sure).
alamz7 5-Jun-13 17:36pm    
well, <%= %> these tags are used for asp.net like the 2nd code snippet above works perfect, I'm just wonder why is the 1st code snippet not working ?
Richard C Bishop 5-Jun-13 17:38pm    
Yes, that does work in ASP.Net, but notice the "#" in front. The tags without that signify classic ASP. Another characteristic that gives it aways is the fact that the tags automatically highlight in yellow.
alamz7 5-Jun-13 17:40pm    
well, the # above is basically a jquery selector, and its not a part of the tags itself,
alamz7 5-Jun-13 17:38pm    
Though one thing I dont know is, what's the difference between <%= %>, <%: %> and <%# %> as I often see them?

1 solution

Please see my comment to the question explaining how an ASP.NET application works with JavaScript. There is no "using", calls between server-side and JavaScript — one is on the server side, another — on the client side.

Now, I'll explain why first code fragment is not working and the second does. This is just JavaScript and jQuery. First of all, TextBox.Text gets hard-coded in the instance of JavaScript text as some text value at the moment. It makes no sense for JavaScript, as this is some arbitrary blah-blah, whatever the value is. It could be
JavaScript
var value = 
// or some
JavaScript
var value = blah-blah

Yes, just so, even without quotation marks. I hope you can see it now. Not only it is gibberish to JavaScript, it may not even compile. JavaScript exceptions are usually hidden from the user, so you can imagine the result.

In second case, jQuery is used with id selector. Actually, TextBox1.ClientID generates some HTML id used in HTML, like in
HTML
<input type=text id="someId" ... />

In your second sample, with the actual id value like in my example above, the JavaScript will read:
JavaScript
var value = $('#someId').val();

This is a valid way to get a jQuery wrapper over HTML DOM object ($('#someId'); an element is found by its unique id value), and call its method .val(), to get the current value.

See also:
http://api.jquery.com/category/selectors/[^],
http://api.jquery.com/id-selector/[^].

If you need to learn jQuery (highly recommended), please see:
http://en.wikipedia.org/wiki/JQuery[^],
http://jquery.com/[^],
http://learn.jquery.com/[^],
http://learn.jquery.com/using-jquery-core/[^],
http://learn.jquery.com/about-jquery/how-jquery-works/[^] (start from here).

—SA
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900