Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9039129
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T09:39:34+00:00 2026-06-16T09:39:34+00:00

I am trying to pass a JavaScript variable to a server-side tag to avoid

  • 0

I am trying to pass a JavaScript variable to a server-side tag to avoid a post-back.

I’d like the solution to look something like this:

Code Behind:

Public Property NameCollection As Dictionary(Of String, String)

JavaScript that I wish to have:

$('#<%= txtIDNumber.ClientID %>').change(function(){
    var idNumber = $('#<%= txtIDNumber.ClientID %>').val();
    var lblName = $('#<%= lblName.ClientID %>');

    <%If NameCollection.ContainsKey(idNumber) Then %>
        lblName.html('<%=NameCollection(idNumber)%>');
    <%Else%>
        lblName.html('<span class="error">Not a User</span>');
    <%End If %>
});

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?

Extra Info:
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. 🙂

EDIT FOR CLARIFICATION:

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 snapshot 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.

$('#<%= txtIDNumber.ClientID %>').change(function(){
    var idnumber = $('#<%= txtIDNumber.ClientID %>').val();
    var lblName = $('#<%= lblName.ClientID %>');
    var tempJSKey = '';
    var tempJSValue = '<span class="error">Not a Member</span>';

    <%If Not NameCollection Is Nothing Then%>
        <%For Each kvp As System.Collections.Generic.KeyValuePair(Of String, String) In NameCollection %>
            tempJSKey = '<%=kvp.Key %>';
            if (tempJSKey == idnumber)
            {
                tempJSValue = '<%=kvp.Value%>';
            }
        <%Next%>
    <%End If%>
    lblName.html(tempJSValue);
});
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T09:39:35+00:00Added an answer on June 16, 2026 at 9:39 am

    The only way to do what you want here is with Ajax or forms.

    $('#<%= txtIDNumber.ClientID %>').change(function(){
        var idNumber = $('#<%= txtIDNumber.ClientID %>').val();
        var lblName = $('#<%= lblName.ClientID %>');
    
        $.ajax({
           "url": "YourServerURLHere",  
           "data":{
               "idNumber":idNumber
           },
           "success": function(data)
           {
              lblName.text(data);
           }
        });
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to pass a JavaScript variable to the server-side using jquery.ajax method. I'm
I'm trying to pass this associative array to JavaScript: Array ( [0] => Array
I am trying to pass a variable in javascript. I create a link in
I'm trying to pass the name of a javascript function to a global variable.
How can one pass a variable to a filter function in javascript? In this
i'm trying to pass a javascript variable into the ruby on rails 「 url_for」method
I am trying to pass a value to a global variable. This is where
I'm trying to create a UTC date in JavaScript on one server, and pass
I'm using rails version 3.2.6 and I'm trying to pass a Javascript variable to
I'm trying to pass a String variable into my javascript function, but my variable

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.