Link to home
Start Free TrialLog in
Avatar of brgdotnet
brgdotnetFlag 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");
 //    });
SOLUTION
Avatar of Dale Burrell
Dale Burrell
Flag of New Zealand image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Avatar of leakim971
leakim971
Flag of Guadeloupe image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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

Avatar of 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