Click here to Skip to main content
11,492,523 members (62,868 online)
Rate this: bad
good
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
Edited 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 at 3-Jan-13 10:35am
   
+5.
Sergey Alexandrovich Kryukov at 3-Jan-13 11:25am
   
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" />
 

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

 <asp:label id="lblRvalue" runat="server"></asp:label>
 

 

    </asp:button></div>
    </form>
 

 





 

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

This is what I wrote in my reply.
PotladaSatish at 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 at 2-Jan-13 9:42am
   
you must have copied the code from my reply. I will update the HTML for hidden field.
Zafar Sultan at 2-Jan-13 9:46am
   
Use this: <asp:HiddenField ID="myHiddenField" runat="server" />
PotladaSatish at 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
0 Sergey Alexandrovich Kryukov 399
1 Maciej Los 266
2 KarstenK 210
3 CPallini 208
4 Sascha Lefèvre 195
0 Sergey Alexandrovich Kryukov 9,943
1 OriginalGriff 8,610
2 Sascha Lefèvre 3,609
3 Maciej Los 3,372
4 Richard Deeming 2,440


Advertise | Privacy | Mobile
Web03 | 2.8.150520.1 | Last Updated 2 Jan 2013
Copyright © CodeProject, 1999-2015
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