你如何跟踪在ASP.NET复选框的变化? [英] How do you keep track of checkbox changes in ASP.NET?

查看:89
本文介绍了你如何跟踪在ASP.NET复选框的变化?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个GridView有许多行,每行有两个复选框,一个是美国的可用性和另一个英国的可用性。之前,code使得它使每一个复选框的变化,网页会做回发,和code,以更新该项目将运行。你可以想像,数百行,任何重大变化将需要很长的时间,它通过这种方法就变得极为乏味。

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.

所以,我创造了一些jQuery的听众对复选框的变化,而且基本上这些方法的点击复选框的索引添加到基于自创建页面到复选框所作的更改现有的JavaScript数组。

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);
  });

现在我不知道如何得到这个信息传回服务器来运行SQL查询,并研究它一点点,我不敢肯定这是最好的解决方案。

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 = [];

我如何在C#中的c-背后传达这一信息$ C $?

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

推荐答案

您可以通过AJAX做到这一点:

you can do it by ajax:

在您aspx.cs文件中添加的WebMethod:

in your aspx.cs file add WebMethod:

    [WebMethod]
    public static int SaveArray(int[] usRowsChecked,int[] usRowsUnchecked, int[]ukRowsChecked, int[] ukRowsUnchecked )
    {
        //do here whatever you want with values
    }

在你的aspx文件(JavaScript)的添加方法如下图所示:

in your aspx file (javascript) add method like below:

$.ajax({
    type: 'POST',
    data: "{usRowsChecked:" + usRowsChecked + 
    ", usRowsUnchecked :" + usRowsUnchecked +
    ", ukRowsChecked :" + ukRowsChecked +
    ", ukRowsUnchecked :" + ukRowsUnchecked +"}",
    contentType: 'application/json; charset=utf-8',
    dataType: 'json',
    async: true,
    url: "YourPage.aspx/SaveArray",
    success: function (result) {
        alert("success");
    },
    error: function (msg) {
        alert("error");
    }
});

这篇关于你如何跟踪在ASP.NET复选框的变化?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆