Click here to Skip to main content
15,389,430 members
Please Sign up or sign in to vote.
3.67/5 (2 votes)
See more:
I have searched around for how to Pass javascript variable's value to C# code behind, but have not got no good result. The key is how to assign an object ID to the variable and then its value can be passed to C# code behind. Appreciate if you can share your experience.
Karthik_Mahalingam 9-Jan-14 13:07pm
post your code..
Mohammad Reza Emami Zadeh 11-Oct-15 9:30am

You cannot interact between client side and server side without sending and HTTP request to the server. You can do it using Ajax:[^].

One convenient way of using Ajax is using jQuery .Ajax():[^].

As Sergey said you cannot assign as javascript variable directly to code behind,
what you can do is like

create a hidden field variable in the client side as

<asp:HiddenField ID="hdnfldVariable" runat="server" />

and in the javascript you can assign some value to the hidden variable as

<script type="text/javascript">
        var somefunction = function () {
            var hdnfldVariable = document.getElementById('hdnfldVariable');
            hdnfldVariable.value = 'value from javascript';

and you can read the hidden field variable in the code behind as

string variable = hdnfldVariable.Value;
[no name] 9-Jan-14 13:36pm
Since I use dojo, I have to use HTML controls. If I use ASP.Net control, get error:
Control 'Button1' of type 'Button' must be placed inside a form tag with runat=server.
Any hint to solve this conflict? Thanks.
Paramaa 9-Jan-14 14:01pm
Do you try this change <form> tag to <form runat="server"> ?
Member 10531790 5-Jun-15 9:38am
[no name] 9-Jan-14 14:07pm
Yes. In my another thread:, I got NullException. Could you help on that one? Thanks.
Karthik_Mahalingam 10-Jan-14 0:05am
replied over there, pls chk it.
[no name] 10-Jan-14 10:44am
Karthik: Done per your advice. Thanks a lot.
Karthik_Mahalingam 10-Jan-14 10:57am
Member 11440780 10-Nov-15 2:24am
is it possible to do the same procedure in mvc C#? as i tried this solution but when i read the variable value like this "string variable = hdnfldVariable.Value;" it gives error that hdnfldVariable is undefined
Karthik_Mahalingam 10-Mar-16 13:01pm
in mvc also the same procedure, instead u will using form object to read the client controls value.
let me know if you have any doubts.
Member 13098156 14-Feb-20 11:22am
Dear Karthik,
I have the same problem: could you please make an example about your answer?

what do you mean for "u will using form object to read the client controls value."

I have:

form id="form1" runat="server"


<asp:hiddenfield id="HiddenField1" runat="server" clientidmode="Static" validaterequestmode="Enabled" viewstatemode="Enabled">

How can i access it from code behind?

string test = form1.HiddenField1.value; does not work at all!

Karthik_Mahalingam 14-Feb-20 21:56pm
ASP or mvc
Member 13098156 17-Feb-20 4:17am
Can you make both examples please?

In the meanwhile I have made some experimentations and in javascript the following lines work properly and they assign the value to the HiddenField1 placed into the .aspx page:

var id= "1";
$("#HiddenField1").val(id); //HiddenField1 is now "1" works!

In my case the issues are (to be a bit more precise):

1) In the code behind
string test = form1.HiddenField1.value does not work (error: HtmlForm does not contains a definition for HiddenField1)
2) In the code behind
string test = HiddenField1.Value seems to be accepted (no error messages) but test is always an empty string, despite the fact that $("#HiddenField1").val(id) is working properly a nutshell:
1) How can I access from code behind to
string test = HiddenField1.Value

2) if I have a method like this in the code behind
public string getState (string id)
string tempvalue = "";
if (state == "1"){tempvalue = "image1.png"}
if (state == "2"){tempvalue = "image2.png"}
return tempvalue;
how can I pass the value id to the method from the .aspx page?

In the .aspx page the statement
var id = 1;
var icon = "<%getState (id)%>";
gives the error "The name 'id' does not exist in the current context".

Hoping I have been clear, thanks in advance for your support.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900