Click here to Skip to main content
12,627,681 members (33,700 online)
Rate this:
 
Please Sign up or sign in to vote.
See more: C# ASP.NET
Javascript:
<script>
function GetValue() {
return 10;
}
</script>

C#.NEt

protected void Page_Load(object sender, EventArgs e)
{
int Data
ClientScript.RegisterStartupScript(this.GetType(), "GetData", "a=GetValue();alert(a)", true);
Data=a;//a is Javascript Variable
}

In C# Page Load Method How to set Javascipt return value to C# Variables.
Posted 1-Jan-13 19:34pm
Updated 2-Jan-13 4:07am
v4
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

Your JavaScript code is executed in a Web browser, only on the client site. And you C# code behind is executed only on server side, only in response to some HTTP request; these two parts of code never call one another. Right? Right!

Conclusions? You need to generate an HTTP request on client side via your JavaScript and process it on server side the way you like. There won't be any other way. In principle, the HTTP requests are generated be HTML forms, but in this case, probably the only adequate technique would be using Ajax. Please see:
http://en.wikipedia.org/wiki/Ajax_%28programming%29[^],
http://www.w3schools.com/ajax/default.asp[^].

There are different ways and frameworks used to leverage Ajax in your Web project, and in ASP.NET in particular. You can consider then all, but I would rather advice one: using jQuery. Please see:
http://en.wikipedia.org/wiki/JQuery[^],
http://jquery.com/[^].

Here is how you can use Ajax via jQuery: http://api.jquery.com/category/ajax/[^].

If you need to learn jQuery (which I would highly recommend), please start here:
http://docs.jquery.com/Tutorials:How_jQuery_Works[^],
http://docs.jquery.com/Tutorials[^].

Happy New Year!

—SA
  Permalink  
Comments
Marcus Kramer 3-Jan-13 10:35am
   
+5.
   
Thank you, Marcus.
—SA
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 2

Hi

Try this,



//inside head tag

<script type="text/javascript">
        function test() {
            if (confirm("test")) {
                document.getElementById('<%=hdnTestValue.ClientID %>').value = "true";
            }
            else {
                document.getElementById('<%= hdnTestValue.ClientID %>').value = "false";
            }
        }
    </script>



<!-- Body Tag -->
 
    <form id="form1" runat="server">
    <div>
    <asp:button runat="server" id="Button1" onclientclick="test()" xmlns:asp="#unknown">
            onclick="Button1_Click" Text="Submit" />




//Code behind

 

  protected void Button1_Click(object sender, EventArgs e)
    {
        lblRvalue.Text = hdnTestValue.Value;
    }



Assign your value to hidden field with JavaScript and pass hidden field value to server side.



Please Mark as Answer if find helpful..

Happy coding...
  Permalink  
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 3

Add a hidden field in your aspx page as:
<asp:HiddenField ID="myHiddenField" runat="server" />
In your JavaScript function use:
<script>
function GetValue() {
document.getElementById('myHiddenField').value = "10";
}
</script>

In your pageload event:
int Data = Convert.ToInt32(myHiddenField.Value);
  Permalink  
v2
Comments
PotladaSatish 2-Jan-13 7:57am
   
I want the javascript assign value in c# pageload method
Zafar Sultan 2-Jan-13 8:03am
   
And what's the problem with the above solution? It does exactly what you want.
PotladaSatish 2-Jan-13 9:09am
   
Yes..It Did.But in i want javascript return value in c# Page Load Method.
Zafar Sultan 2-Jan-13 9:12am
   
In your pageload event:
int Data = Convert.ToInt32(myHiddenField.Value);

This is what I wrote in my reply.
PotladaSatish 2-Jan-13 9:39am
   
I am getting this error :

'System.Web.UI.WebControls.HiddenField' does not have a public property named 'xmlns:asp'.
Zafar Sultan 2-Jan-13 9:42am
   
you must have copied the code from my reply. I will update the HTML for hidden field.
Zafar Sultan 2-Jan-13 9:46am
   
Use this: <asp:HiddenField ID="myHiddenField" runat="server" />
PotladaSatish 3-Jan-13 0:05am
   
I copied all code what you wrote,i got value in hidden field after ready document but in page load i am getting empty data.
Thanks for giving reply to me sir.

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.161205.3 | Last Updated 2 Jan 2013
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