How to get c# code behind variable value in javascript? I have declared a variable in page load. I want that variable value in javascript.
Javascript
Code C#
You cannot interact between client-side and server-side without sending an HTTP request to the server.
you cannot assign as javascript variable directly to the code-behind.
what you can do is like
create a hidden field variable in the client side as
<asp:HiddenField ID="hfVariable" runat="server" />
and in the javascript, you can assign some value to the hidden variable as
<script type="text/javascript">
var somefunction = function () {
var hfVariable = document.getElementById('<%= hfVariable.ClientID %>');
hfVariable.value = 'value';
}
</script>
and you can read the hidden field variable in the code behind as
string variable = hfVariable.Value;