Skip to content Skip to sidebar Skip to footer

How To Pass Javascript Variable To Server Side Asp.net

I am trying to write javascript variable(hdnField) to server side. In javascript code I have assigned the value of 'HiddenField' Control and then I want to write this value to ser

Solution 1:

I don't know where

<divclass="left"><%Response.Write(hdnField.Value);%></div>

Comes into play because that looks more ASP than ASP.NET web forms, but if you have:

<asp:hiddenfield id="hdnField" runat="server" ></asp:hiddenfield>

You can read and write to this on the client via:

document.getElementById('<%= hdnField.ClientID %>').value = 'XYZ';

alert(document.getElementById('<%= hdnField.ClientID %>').value);

On the server, you can read and write to this via:

hdnField.Text = "XYZ";

var text = hdnField.Text;

As long as hdnField is not in a template or list control, you can refer to it directly on the server, with the <asp:HiddenField> control you can do both. This bridges the gap so that you can change the value on the client, then retrieve the value on postback. AJAX is only necessary if you need to send the value to the server before the next postback occurs, or out of the normal flow of the ASP.NET postback lifecycle.

Post a Comment for "How To Pass Javascript Variable To Server Side Asp.net"