Click here to Skip to main content
15,890,512 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I tried to pass a variable from the client side to server side using HiddenField like in this example: asp.net - pass client side javascript value to server side C# - Stack Overflow

What I have tried so far:
HTML
<!DOCTYPE html>

    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title></title>
        <script type ="text/javascript"> 
         <script type ="text/javascript"> 
                function passValue() {
                    document.getElementById('<%=HiddenField1.ClientID%>').value = "some stuff";
                }
                 window.onload = passValue;

            </script>
    </head>
    <body>    
        <form id="form1" runat="server">
        <div>    
            <asp:HiddenField ID="HiddenField1" runat="server"/>

            <asp:Button ID="Button1" runat="server" Text="Button" OnClientClick ="passValue()"/>

        </div>
        </form>
    </body>
</html>


On the server side I have the following code:
C#
protected void Page_Load(object sender, EventArgs e)
        {
            string a, b;
            if (IsPostBack)
            {
                Response.Write("The input value is: " + HiddenField1.Value.ToString());
                a = HiddenField1.Value.ToString();
            }
            else
            {
                Response.Write("The input value is: " + HiddenField1.Value.ToString());
                b = HiddenField1.Value.ToString();
            }
        }


When my page opens I expect to see on it the message: "The input value is: some stuff". The message appears only if I press the button because only in this case the IsPostBack becomes true. What should I do so get the message without pressing the button? Thanks in advance!

What I have tried:

In the above section can be seen what I have tried so far.
Posted
Updated 19-May-16 22:35pm
Comments
CHill60 20-May-16 3:59am    
When you first load your page HiddenField1 has no value (because you have not initialised it to anything). It is only getting a value in button1 OnClientClick.
Try <asp:HiddenField ID="HiddenField1" runat="server" value="initial value"/>
F-ES Sitecore 20-May-16 5:18am    
You need to understand the asp.net page lifecycle and the order things happen. When you request the page your page_load and code-behind runs first, they generate html which is then sent to the client. You can't have js on the client influence what happens on the server as the server code runs first.

1 solution

To get the message as you expected, then you will have to write the code like this
ASP.NET
<asp:HiddenField ID="HiddenField1" Value="some stuff" runat="server" />


Since you are assigning the value to the hidden field in Client Side, the value can be accessed only on the PostBack

if you are assigning the value to hidden field in the mark up then you can get the value on Page Load ( not postback )

change the value as below to see how it works
JavaScript
document.getElementById('<%=HiddenField1.ClientID%>').value = "some other more stuff";


Now while the page is loaded for first time you will see the output as "The input value is :some stuff"
after post back the output will be "The input value is "some other more stuff"
 
Share this answer
 
Comments
DrgIonuţ 23-May-16 4:11am    
It works! My question is next: is there any way to place instead of Value="some stuff" let's say for example the width of a div? I store the div's width in a var called dv. How could I pass the dv's value? I tried Value= dv, but what I get is "dv", not the actual value of dv. Thanks in advance!
Karthik_Mahalingam 23-May-16 4:15am    
in many ways you can do that..
one of the way, wich is similar to the above
create a hiddenfield for storing the div width value

var dv = '200px'
document.getElementById('<%=HiddenField1.divId %>').value = dv;

else you can make the div as server control by adding an attribute ( runat="server" ) and you can acces the properties/Attributes directly in the code behind..
DrgIonuţ 23-May-16 4:19am    
What I tried is:
var dv= document.getElementById('<%= HiddenField1.ClientID %>');
and div is:
<div id="myDiv" style="width:100%; background-color:red">


For the second option is no longer needed HiddenField?
Karthik_Mahalingam 23-May-16 4:23am    
for second option, hiddenfield not necessary.


and correction to your code..
var dv= document.getElementById('myDiv').style.width;
document.getElementById('<%= HiddenField1.ClientID %>').value = dv;

DrgIonuţ 23-May-16 4:31am    
So. I have this:
var dv = document.getElementById('myDiv').style.width;
document.getElementById('<%= HiddenField1.ClientID %>').value = dv;
<asp:HiddenField ID="HiddenField1" runat="server" Value="???"/>
On ther server side I have:
string myHiddenValue1 = this.HiddenField1.Value;
Response.Write(myHiddenValue1);

What I'm doing wrong here?

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