Click here to Skip to main content
15,891,033 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..
Posted

Add this in ur design page:

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


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


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

string codeBehindValue = hdnResultValue.Value;
 
Share this answer
 
Comments
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# ????
XML
Hi,
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() {
debugger;
var val = 'Name:Tajuddin';
return val;
}
</script>

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>");
 
Share this answer
 
Comments
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
code returns country code, but how to receive javascript values in Asp.net(C#) 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()
{
debugger;
var hfValue = document.write(geoip_country_code());
alert(hfValue)
document.getElementById('hidcountry').value=hfValue.value;
return true;
}







--------------------------------------------

step 2





var hfValue = document.write(geoip_country_code());
alert(hfValue)
document.getElementById('hidcountry').value=hfValue.value;
return true;






 
Share this answer
 
Comments
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[^]
 
Share this answer
 
Comments
Ade.Ruyani.Z 21-Jul-11 0:52am    
Thank you raiskazi..

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

Check the below code

Designer

<<pre lang="xml">%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Untitled Page</title>
    <script type="text/javascript">
        function setvalue()
        {
            debugger;
            var hfValue = document.getElementById('HiddenField1');
            hfValue.value = 'Hieeee...';
            return true;
        }
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <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>
    </form>
</body>
</html>


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;
    }
}

 
Share this answer
 
v2
Comments
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 solution...is 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