Avatar of brgdotnet
brgdotnet
Flag for United States of America asked on

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

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");
         });          
           
     </script>  

// This code works, if I hard code a value. However I need to send the value back from C#
 //$(document).ready(function()
 //   {
 //     $("#<%=MaskedTextBox.ClientID%>").val("30398765437812").mask("(999) 999-9999 Ext.9999");
 //    });
ASP.NETjQuery

Avatar of undefined
Last Comment
brgdotnet

8/22/2022 - Mon
SOLUTION
Dale Burrell

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
ASKER CERTIFIED SOLUTION
leakim971

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
Kiran Sonawane

Try with this

<script type="text/javascript">
    $(document).ready(function () {
        $("#<%=txtPhone.ClientID%>").val($("#<%= hiddenLabel.ClientID %>").val()).mask("(999) 999-9999 Ext.9999");
    });          
           
</script> 

Open in new window

brgdotnet

ASKER
Basically this article sums up my dilemna of the best approach. I found it tonight after testing lakims solution, which worked.

http://programmers.stackexchange.com/questions/122357/how-should-data-be-passed-between-client-side-javascript-and-c-code-behind-an-a
I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck