In this article I will explain with an example, how pass (return) value from JavaScript function to Code Behind (Server Side) function in ASP.Net using C# and VB.Net.
The returned value from JavaScript function will be saved in a HiddenField and then will be sent to Code Behind (Server Side) function through PostBack (Form Submission) in ASP.Net using C# and VB.Net.
HTML Markup
The HTML Markup consists of an ASP.Net Button btnConfirm. The Button has been assigned an OnClick and OnClientClick event handler.
When the Button is clicked, the OnClientClick event will trigger the JavaScript Confirm method.
Inside the JavaScript Confirm method, the input provided by the user is stored in a dynamically created hidden field i.e. If OK is pressed value Yes is stored and if Cancel is pressed No is stored, so that we can pass the user inputs onto server side code.
Then the Button does normal PostBack and raise the OnClick event handler.
<html xmlns="">
    <script type = "text/javascript">
        function Confirm() {
            var confirm_value = document.createElement("INPUT");
            confirm_value.type = "hidden";
   = "confirm_value";
            if (confirm("Do you want to save data?")) {
                confirm_value.value = "Yes";
            } else {
                confirm_value.value = "No";
    <form id="form1" runat="server">
      <asp:Button ID="btnConfirm" runat="server" OnClick="OnConfirm" Text="Raise Confirm" OnClientClick="Confirm()"/>
Fetching the User input in server side
Inside the OnConfirm Click event handler, the user input is fetched that was stored in the dynamic hidden field from the Request.Form collection.
Then based on whether user has selected OK or Cancel different message is displayed using JavaScript Alert Message Box.
public void OnConfirm(object sender, EventArgs e)
    string confirmValue = Request.Form["confirm_value"];
    if (confirmValue == "Yes")
        this.Page.ClientScript.RegisterStartupScript(this.GetType(), "alert""alert('You clicked YES!')"true);
        this.Page.ClientScript.RegisterStartupScript(this.GetType(), "alert""alert('You clicked NO!')"true);
Protected Sub OnConfirm(sender As Object, e As EventArgs)
    Dim confirmValue As String = Request.Form("confirm_value")
    If confirmValue = "Yes" Then
        ClientScript.RegisterStartupScript(Me.[GetType](), "alert""alert('You clicked YES!')"True)
        ClientScript.RegisterStartupScript(Me.[GetType](), "alert""alert('You clicked NO!')"True)
    End If
End Sub
The JavaScript Confirmation Box
Pass (Return) value from JavaScript function to Code Behind (Server Side) in ASP.Net
The JavaScript Alert Box displaying the response chosen
Pass (Return) value from JavaScript function to Code Behind (Server Side) in ASP.Net
Browser Compatibility

The above code has been tested in the following browsers.

Internet Explorer  FireFox  Chrome  Safari  Opera 

* All browser logos displayed above are property of their respective owners.