Click here to Skip to main content
15,173,031 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
hello.. i'm junior in ASP.NET.

i want to ask,, if i create function with return value in a javascript(example return value of a string), can i get a value from code behind with C#?

please your advice and fewer or more samples.

Thank you..

Add this in ur design page:

<asp:HiddenField ID="hdnResultValue" Value="0" runat="server" />
        <asp:Button ID="Button1" runat="server"  OnClientClick="returnString();"
            Text="Button" onclick="Button1_Click" />

script for this:
<script language="javascript" type="text/javascript">
       function returnString() {
           document.getElementById("hdnResultValue").value = "Tajuddin";

Now get the value of hiddenfield in code behind wherever u want like below:

string codeBehindValue = hdnResultValue.Value;
Ade.Ruyani.Z 21-Jul-11 4:07am
hi tajuddin,, thank you for your help and sample code.
Member 11942465 18-Sep-15 8:08am
Hi Tajuddin, i want also get value from html to .asmx page in c# but my html code in notepad++ then how to access variable value in c# ????
Here is the solution for your question , yeah u can get the value from code behind of js function,

see the following example:

javascript function which returns string

<script language="javascript" type="text/javascript">
function returnString() {
var val = 'Name:Tajuddin';
return val;

C# Code to get the return value of the above function:

ClientScript.RegisterClientScriptBlock(this.GetType(), "alertScript", "<script language=javascript>var a=returnString();alert(a);</script>");
shefeekcm 21-Jul-11 0:42am
what is the use of "debugger" here...?
Tajuddin_HYD 21-Jul-11 0:57am
Hi,by using debugger we can debugg script code line by line
Ade.Ruyani.Z 21-Jul-11 0:50am
thank you tajudin,, can i ask something??

what function "debugger;"? if i disable that script is not working?

second, how i can get value and put in variable from code behind?

example i want to put the value in string result = "..."

thank you for your help..
Tajuddin_HYD 21-Jul-11 0:56am
Hi, debugger used in javascript by using this we can debugg script line by line
Tajuddin_HYD 21-Jul-11 0:58am
Hi aruza here is no matter of debugger it is just used to debug the script code line by line thats it
Ade.Ruyani.Z 21-Jul-11 1:04am
oww... maybe I am wrong in carry out the code,, sorry for that.

oh ya tajudin,, can i get value and put in variable from code behind?

example i want to put the value in string result = "..."?

sorry if i ask many question? ^_^
Tajuddin_HYD 21-Jul-11 2:11am
Hi aruza sorry for delay....
i got what exactly u want to do,
Take one hidden field and assign the result in js function and get it in code behind page
i will post one more example code go through that
Bassem Yehia Salama 21-Jan-15 9:03am
can you plz submit the code to assign the value to code behind var
Member 8106781 23-Jul-11 10:36am
ok tajuddin,, it's ok.

thanks for your help code and explanation.
code returns country code, but how to receive javascript values in variable ?

i have taken a hidden field which control id is - hidcountry, but control id does not receive values.
please help me.

step 1
using function

function setvalue()
var hfValue = document.write(geoip_country_code());
return true;


step 2

var hfValue = document.write(geoip_country_code());
return true;

CHill60 25-Jan-14 12:14pm
Posting a question as a solution to someone elses post will not get you much help. Use the "Ask a question" link to post your own question and use "pre" tags to format your code when you do so
Yes you can do this by using Ajax. Have a look at below link.

Simplifying Asp.Net Core Ajax[^]
Ade.Ruyani.Z 21-Jul-11 0:52am
Thank you raiskazi..

this article help my comprehension,,
[no name] 21-Jul-11 4:39am
Perfect !
Yes you can...

Check the below code


<<pre lang="xml">%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "">
<html xmlns="" >
<head runat="server">
    <title>Untitled Page</title>
    <script type="text/javascript">
        function setvalue()
            var hfValue = document.getElementById('HiddenField1');
            hfValue.value = 'Hieeee...';
            return true;
    <form id="form1" runat="server">
        <asp:HiddenField ID="HiddenField1" runat="server" Value="Hie.." />
        <asp:Button ID="Button1" runat="server" Text="Show HiddenValue" OnClientClick="return setvalue();" OnClick="Button1_Click" />
        <asp:Label ID="Label1" runat="server"></asp:Label></div>

Code behind
<pre lang="cs">using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
public partial class _Default : System.Web.UI.Page
    protected void Page_Load(object sender, EventArgs e)
    protected void Button1_Click(object sender, EventArgs e)
        Label1.Text = HiddenField1.Value;

Member 8106781 23-Jul-11 10:38am
ok nadar,, thanks for your sample code and with your help I got quite a lot with understanding.
Avinash_Pathak 13-Dec-12 23:48pm
hello Muthukumar...this is working but in my application there is not any button or hidden field in aspx page....I want this value in string which is on .cs file....I am searching this since last 3 days but still not found proper it possible... ??? if yes then please help me... thanks in advance...

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