2

How can I return a true/false from a JavaScript function to C# code?

What is the problem with my code?

ASP.NET

< asp:HiddenField ID="hiddenFieldResult" runat="server" />

JavaScript

< script type="text/javascript">
< !--
function deleteUser(userName) {
var retVal = confirm("Delete " + userName + "?");
if (retVal == true) {
  document.getElementById('<% =hiddenFieldResult.ClientID %>').value = true;
  return true;
 }
else {
  document.getElementById('<% =hiddenFieldResult.ClientID %>').value = false;
  return false;
 }
}
//-->
</script>

C#

Page.ClientScript.RegisterStartupScript(this.GetType(), "alert", "deleteUser('" + user.userName + "');", true);
Label1.Text = "User is deleted? " + hiddenFieldResult.Value;
2
  • 9
    You need to understand that difference between client-side code and server-side code. That's totally impossible; you need multiple HTTP requests. Commented Feb 6, 2014 at 17:11
  • Run it in the debugger - you'll see that those two C# lines are executed before the page is shown in the browser and the script executes. Commented Feb 6, 2014 at 18:33

1 Answer 1

2

SLaks is right. You can't just pass values from JavaScript to C# that way. What I usually do for simple cases when I need to share data between JavaScript (client-side code) and C# (server-side code) is create an ASPX file that will recieve the JavaScript variables.

For example, in my HTML I have this:

var Id = $(this).data("Id");
$.ajax({
    type: "POST",
    url: "catcher.aspx/doSomething",
    data: '{ Id: ' + Id + ' }',
    contentType: "application/json; charset=utf-8",
    dataType: "json",
    async: false,
    success: function (data) {
        // Do something on success
        console.log(data.d);
        // data.d returns "Hello World!"
    },
    failure: function (response) {
        // Do something on failure
    }
});

With this I'm sending and AJAX POST to a file called catcher.aspx, which has a WebMethod called doSomething in it's codebehind file. Which looks like this:

namespace myNamespace
{
    public partial class functions : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
        }

        [WebMethod()]
        public static string doSomething(string Id)
        {
            string something = "Hello World!";
            return something;
        }
    }
}

To wrap up. The JavaScript calls the method in C# through an AJAX POST. It sends and Id and gets a string in return. You can tweak both the JavaScript and C# to your needs.

If you're trying yo send a boolean to C#, POST it.

function deleteUser(userName) {
    var retVal = confirm("Delete " + userName + "?");
    if (retVal == true) {
        document.getElementById('<% =hiddenFieldResult.ClientID %>').value = true;
        // POST true to example.aspx doSomething method
    } else {
        document.getElementById('<% =hiddenFieldResult.ClientID %>').value = false;
        // POST false to example.aspx doSomething method
    }
}

Catch it in example.aspx.cs

namespace myNamespace
{
    public partial class functions : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
        }

        [WebMethod()]
        public static string doSomething(boolean Id)
        {
            string something = "Hello World!";
            return something;
        }
    }
}

By the way, this requires jQuery. Hope it helped.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.