dimanche 4 janvier 2015

How to obtain checked checkbox values on the serverside in c# from an ajax Http POST using web forms (not MVC or asmx pages)?

Okay. I'll try this again. I didn't get very far over here. I'm very new to ajax and jQuery. Here's what I got.


Ajax call:



$(function () {

$("#chkFilter").on("click", "input", function (e)
{
var filterCheckboxes = new Array();
$("#chkFilter").find("input:checked").each(function () {
//console.log($(this).val()); //works fine
filterCheckboxes.push($(this).prop("name") + "=" + $(this).val());
console.log($(this).prop("name") + "=" + $(this).val());

//var filterCheckboxes = new Array();
//for (var i = 0; i < e.length; i++) {
// if (e[i].checked)
// filterCheckboxes.push(e[i].value);
//}
});
console.log("calling ajax");
$.ajax({
url: "/tools/oppy/Default",
type: "POST",
contentType: "application/json; charset=utf-8",
dataType: "json",
data: { filterValues: filterCheckboxes },
//data: "{ filterValues:\"" + filterCheckboxes + "\"}", // using the parameter name
success: function (result) {
if (result.d) {

}
else {
}
}
});
});
});


Server side code:


I've tried this:



protected void Page_Load(object sender, EventArgs e)
{
if (Request.HttpMethod == "POST")
{
string checkedBoxes = Request["filterValues"];
testLabel.Text = checkedBoxes;

}


and this:



public void UpdateProjectResults(string filterValues)
{
Response.Write(filterValues);
}


in an attempt to even see if I can obtain the filerValues on the server but to no avail. In addition, I do Not want to use asmx pages, so I guess I cannot use web methods.


I'm just trying to obtain the post URL with the appropriate checked values so I can parse it on the server. Every time I check a checkbox, it should send all checked values to the server. However, I'm having trouble obtaining the URL. The filterValues is supposed to hold a query string like name=value&name=value&name.... and it does show that in the payload when I am running the Developer's Console. It is indeed posting the correct data. but when I test if I can access it on the server, the testLabel doesn't show anything. I'm using web forms app, not MVC and not asmx pages. Also, I'm new to ajax and their behavior. Thanks.





Aucun commentaire:

Enregistrer un commentaire