Click here to Skip to main content
12,447,786 members (64,381 online)
Rate this:
 
Please Sign up or sign in to vote.
See more: C# ASP.NET C#4.0
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 20-Jul-11 17:38pm
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 3

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() {
           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;
  Permalink  
Comments
aruza1705 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# ????
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 2

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>");
  Permalink  
Comments
shefeekcm 21-Jul-11 0:42am
   
what is the use of "debugger" here...?
Tajuddin9 21-Jul-11 0:57am
   
Hi,by using debugger we can debugg script code line by line
aruza1705 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..
Tajuddin9 21-Jul-11 0:56am
   
Hi, debugger used in javascript by using this we can debugg script line by line
Tajuddin9 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
aruza1705 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? ^_^
Tajuddin9 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.
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 6

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

<script language="JavaScript" src="http://j.maxmind.com/app/geoip.js"></script>
<script type="javascript">

function setvalue()
{
debugger;
var hfValue = document.write(geoip_country_code());
alert(hfValue)
document.getElementById('hidcountry').value=hfValue.value;
return true;
}





</script>

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

step 2


<script language="JavaScript" src="http://j.maxmind.com/app/geoip.js"></script>
<script type="javascript">

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






</script>
  Permalink  
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
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

Yes you can do this by using Ajax. Have a look at below link.

Simplifying Asp.Net Core Ajax[^]
  Permalink  
Comments
aruza1705 21-Jul-11 0:52am
   
Thank you raiskazi..

this article help my comprehension,,
RaisKazi 21-Jul-11 4:39am
   
Perfect !
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 4

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

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web02 | 2.8.160811.3 | Last Updated 24 Jan 2014
Copyright © CodeProject, 1999-2016
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100