Click here to Skip to main content
15,886,016 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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
Updated 2-Jan-13 3:07am
v4

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
 
Share this answer
 
Comments
fjdiewornncalwe 3-Jan-13 10:35am    
+5.
Sergey Alexandrovich Kryukov 3-Jan-13 11:25am    
Thank you, Marcus.
—SA
Hi
Try this,



JavaScript
//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>




ASP.NET
<!-- 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>





C#
//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...
 
Share this answer
 
Add a hidden field in your aspx page as:
ASP.NET
<asp:HiddenField ID="myHiddenField" runat="server" />

In your JavaScript function use:
XML
<script>
function GetValue() {
document.getElementById('myHiddenField').value = "10";
}
</script>


In your pageload event:
C#
int Data = Convert.ToInt32(myHiddenField.Value);
 
Share this answer
 
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'.

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