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.