Click here to Skip to main content
11,705,212 members (51,172 online)
Rate this: bad
Please Sign up or sign in to vote.
See more: C# ASP.NET
function GetValue() {
return 10;


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 18:34pm
Edited 2-Jan-13 3:07am
Rate this: bad
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:[^],[^].

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:[^],[^].

Here is how you can use Ajax via jQuery:[^].

If you need to learn jQuery (which I would highly recommend), please start here:[^],[^].

Happy New Year!

Marcus Kramer at 3-Jan-13 10:35am
Sergey Alexandrovich Kryukov at 3-Jan-13 11:25am
Thank you, Marcus.
Rate this: bad
Please Sign up or sign in to vote.

Solution 2


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

<!-- Body Tag -->
    <form id="form1" runat="server">
    <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>



//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...
Rate this: bad
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:
function GetValue() {
document.getElementById('myHiddenField').value = "10";

In your pageload event:
int Data = Convert.ToInt32(myHiddenField.Value);
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 525
1 OriginalGriff 244
2 ppolymorphe 126
3 F-ES Sitecore 110
4 Andy Lanng 85
0 OriginalGriff 8,988
1 Sergey Alexandrovich Kryukov 8,272
2 CPallini 5,189
3 Maciej Los 4,726
4 Mika Wendelius 3,606

Advertise | Privacy | Mobile
Web03 | 2.8.150819.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