brgdotnet
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(txt TheValue .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(functi on()
// {
// $("#<%=MaskedTextBox.Clien tID%>").va l("3039876 5437812"). mask("(999 ) 999-9999 Ext.9999");
// });
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('<
alert(txtTheValue .innerHTML);
$(document).ready(function
$("#<%=txtPhone.ClientID%>
});
</script>
// This code works, if I hard code a value. However I need to send the value back from C#
//$(document).ready(functi
// {
// $("#<%=MaskedTextBox.Clien
// });
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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
http://programmers.stackexchange.com/questions/122357/how-should-data-be-passed-between-client-side-javascript-and-c-code-behind-an-a
Open in new window