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.
Posted
Comments
Karthik_Mahalingam 9-Jan-14 13:07pm
   
post your code..
Mohammad Reza Emami Zadeh 11-Oct-15 9:30am
   
test

You cannot interact between client side and server side without sending and HTTP request to the server. You can do it using Ajax:
http://en.wikipedia.org/wiki/Ajax_%28programming%29[^].

One convenient way of using Ajax is using jQuery .Ajax(): http://api.jquery.com/jquery.ajax/[^].

—SA
   
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.NET
<asp:HiddenField ID="hdnfldVariable" runat="server" />


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

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


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

C#
string variable = hdnfldVariable.Value;
   
Comments
[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
   
ik
[no name] 9-Jan-14 14:07pm
   
Yes. In my another thread: http://www.codeproject.com/Questions/708621/System-Web-UI-Page-Request-Not-there, I got NullException. Could you help on that one? Thanks.
Karthik_Mahalingam 10-Jan-14 0:05am
   
replied over there, pls chk it.
Thanks
karthik
[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"

and

<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!

Regards
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"...it 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

So...in 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