Note that there are some explanatory texts on larger screens.

plurals
  1. POCan I use a JavaScript variable within Server-Side Tags?
    text
    copied!<p>I am trying to pass a JavaScript variable to a server-side tag to avoid a post-back. </p> <p>I'd like the solution to look something like this:</p> <p>Code Behind:</p> <pre><code>Public Property NameCollection As Dictionary(Of String, String) </code></pre> <p>JavaScript that I wish to have:</p> <pre><code>$('#&lt;%= txtIDNumber.ClientID %&gt;').change(function(){ var idNumber = $('#&lt;%= txtIDNumber.ClientID %&gt;').val(); var lblName = $('#&lt;%= lblName.ClientID %&gt;'); &lt;%If NameCollection.ContainsKey(idNumber) Then %&gt; lblName.html('&lt;%=NameCollection(idNumber)%&gt;'); &lt;%Else%&gt; lblName.html('&lt;span class="error"&gt;Not a User&lt;/span&gt;'); &lt;%End If %&gt; }); </code></pre> <p>These server-side tags work properly if I hard-code the idNumber (say '1234' or something), so I know my object/code itself is working properly, but is there a way to get it to recognize/pass the JavaScript variable here?</p> <p><strong>Extra Info:</strong> I do have a functional work-around, but it is the ugly and inefficient method of looping through all the items in the collection, so I would like to know how to do this better if there is a way to. Thanks for help. :)</p> <p><strong>EDIT FOR CLARIFICATION:</strong></p> <p>This is my current work-around. Very ugly because I can't figure out a way to just use the idnumber as an index instead of looping through all items. The NameCollection is already loaded with the data on the ASP.NET page load event, so while I'm not sure what's going on in the background with server-side tags, I assumed the data should exist in that object statically the way it was at the last call to the server, but that the <em>snapshot</em> of that data physically existed on the client-side (it would have to to avoid post-backs, right?)? Thus, I should be able to avoid having to talk to the server here since I am only accessing data that should already be on the client side? Correct me (nicely please) if I am wrong, since I'm not an expert on JavaScript or web programming in general and I have been able to find very few decent references on server-side tags.</p> <pre><code>$('#&lt;%= txtIDNumber.ClientID %&gt;').change(function(){ var idnumber = $('#&lt;%= txtIDNumber.ClientID %&gt;').val(); var lblName = $('#&lt;%= lblName.ClientID %&gt;'); var tempJSKey = ''; var tempJSValue = '&lt;span class="error"&gt;Not a Member&lt;/span&gt;'; &lt;%If Not NameCollection Is Nothing Then%&gt; &lt;%For Each kvp As System.Collections.Generic.KeyValuePair(Of String, String) In NameCollection %&gt; tempJSKey = '&lt;%=kvp.Key %&gt;'; if (tempJSKey == idnumber) { tempJSValue = '&lt;%=kvp.Value%&gt;'; } &lt;%Next%&gt; &lt;%End If%&gt; lblName.html(tempJSValue); }); </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload