troubleshooting Question

How to access C# variable from JQuery? I tried using a hidden variable on form. Not working.

Avatar of brgdotnet
brgdotnetFlag for United States of America asked on
4 Comments2 Solutions4496 ViewsLast Modified:
I am reading a phone number from the database in C#, and want to copy it to a textbox on my web form.
My text box is masked using JQuery.

I don't know how to access C# variables from JQuery or Java Script. So my approach is to have a hidden
variable on my web form. Then in C# I use the text box property of the hiddenlabel control, to set the value of the
hidden label control.

I figure that once the text on my label control is set in C#, that I can then access it in JQuery or Java Script.
Accessing it for the purpose of populating my text box control "txtPhone"

However when I try and access the value for my hidden label control, it is coming back as null. so if I trace into
the JQuery below, the value for "var" is null, before attempting to display it with the alert command.

So why is the value for by label control coming back as null, from my C# code?

If their is a better way to set the textbox from C#, I am open to doing it that way?
Please help!


    <script type="text/javascript">
         var txtTheValue = document.getElementById('<%= hiddenLabel.ClientID %>');
         alert(txtTheValue .innerHTML);

         $(document).ready(function () {
             $("#<%=txtPhone.ClientID%>").val(txtTheValue .innerHTML).mask("(999) 999-9999 Ext.9999");

// This code works, if I hard code a value. However I need to send the value back from C#
 //   {
 //     $("#<%=MaskedTextBox.ClientID%>").val("30398765437812").mask("(999) 999-9999 Ext.9999");
 //    });
Join our community to see this answer!
Unlock 2 Answers and 4 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 2 Answers and 4 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros