jeudi 27 août 2015

How do you keep track of checkbox changes in ASP.NET?

I have a Gridview with many rows, and each row has two checkboxes, one for US availability, and another for UK availability. Before, the code made it so that on every checkbox change, the page would do a postback, and code to update for that item would be run. As you can imagine, for hundreds of rows, any major changes will take a very long time and it becomes extremely tedious through this method.

So I created some jQuery listeners for changes on a checkbox, and these methods essentially add the index of the clicked checkbox to existing Javascript arrays based on what changes were made to that checkbox since the creation of the page.

$('.checkUs input:checkbox').click(function () {
    var row = $(this).parent().parent().parent().index();
    var isChecked = $(this).is(':checked');

    if (isChecked && $.inArray(row, usRowsChecked) === -1 && $.inArray(row, usRowsUnchecked) === -1)
      usRowsChecked.push(row);
    else if (isChecked && $.inArray(row, usRowsUnchecked) !== -1)
      usRowsUnchecked.splice($.inArray(row, usRowsUnchecked), 1);

    if (!isChecked && $.inArray(row, usRowsUnchecked) === -1 && $.inArray(row, usRowsChecked) === -1)
      usRowsUnchecked.push(row);
    else if (!isChecked && $.inArray(row, usRowsChecked) !== -1)
      usRowsChecked.splice($.inArray(row, usRowsChecked), 1);
  });

Now I don't know how to get this information back to the server to run the SQL queries, and from researching it for a little bit, I'm not so sure this is the best solution.

Upon the user clicking the "Save" button, I need to send four arrays to the back-end:

var usRowsChecked = [];
var usRowsUnchecked = [];
var ukRowsChecked = [];
var ukRowsUnchecked = [];

How do I communicate this information to the code-behind in C#?




Aucun commentaire:

Enregistrer un commentaire