如何在服务器端按钮单击处理程序中检索JavaScript数组?

时间:2022-06-11 15:42:25

I have the following jQuery:

我有以下jQuery:

<script>
    $(document).ready(function () {
        $('#btnGetIDs').click(function getIDs() {
            var checkedIds = $(":checkbox:checked").map(function () {
                return this.id;
            }).get();
        });
    });
</script>

This is used for the creation of the checkboxes (it is nested within a lengthy foreach datarow in datatable).

这用于创建复选框(它嵌套在数据表中的一个冗长的foreach数据行中)。

                    results.Append("<td> <input id=" + RosteredCareID + " type=\"checkbox\" unchecked>        </td>");

Which I can see, from debugging in the browser, is working as intended and creating an array of IDs.

从浏览器中调试,我可以看到,它正在按预期工作并创建一个ID数组。

The intended functionality is: 1. System loads page 2. User clicks some check boxes 3. User clicks submit 4. System gets the IDs of checked boxes and stores in array

预期的功能是:1。系统加载页面2.用户单击一些复选框3.用户单击提交4.系统获取复选框的ID和存储在数组中

Here

    protected void btnConfirm_Click(object sender, EventArgs e)
    {
        string[] IDs = new string[20];

        // IDs = "";


    }

So how I can access the jQuery array from the btnConfirm_Click event?

那我怎么能从btnConfirm_Click事件中访问jQuery数组呢?

I have tried a scriptmanager line of code but it was causing errors with a scriptmanager on a masterpage.

我尝试过一个scriptmanager代码行,但它导致了masterpage上的scriptmanager错误。

1 个解决方案

#1


Add a name property to your checkbox markup.

在复选框标记中添加名称属性。

results.Append("<td><input id='" + RosteredCareID + "' type='checkbox' name='" + RosteredCareID  + "' unchecked /></td>");

(Notice I properly quoted your ID. And I used single quotes within the JS string to avoid the need to escape your double quotes.)

(注意我正确引用了你的ID。我在JS字符串中使用了单引号,以避免需要转义双引号。)

Then in the server side, the values will be available in the Request.Form property. You can enumerate over them like this:

然后在服务器端,值将在Request.Form属性中可用。您可以像这样枚举它们:

foreach (var name in Request.Form.AllKeys)
{
    System.Diagnostics.Debug.WriteLine("Form Name: " + name + " Form Value: " + Request.Form[key]);
}

If the value is checked, it will be in there.

如果检查了该值,它将在那里。

There may be lots of form values, so you'll need to filter them to get only what you want. I suggest you prefix your name with a particular string. Ex:

可能有很多表单值,因此您需要过滤它们以获得您想要的内容。我建议你在名字前加上一个特定的字符串。例如:

results.Append("<td><input id='" + RosteredCareID + "' type='checkbox' name='RosterCareCB_" + RosteredCareID  + "' unchecked /></td>");

Then on the server side, filter on that prefix.

然后在服务器端,过滤该前缀。

var checkedIds = new List<string>();
foreach (var name in Request.Form.AllKeys)
{
    if(name.StartsWith("RosterCareCB_") && Request.Form[name] == "on")
    {
        checkedIds.Add(name);
    }
}

#1


Add a name property to your checkbox markup.

在复选框标记中添加名称属性。

results.Append("<td><input id='" + RosteredCareID + "' type='checkbox' name='" + RosteredCareID  + "' unchecked /></td>");

(Notice I properly quoted your ID. And I used single quotes within the JS string to avoid the need to escape your double quotes.)

(注意我正确引用了你的ID。我在JS字符串中使用了单引号,以避免需要转义双引号。)

Then in the server side, the values will be available in the Request.Form property. You can enumerate over them like this:

然后在服务器端,值将在Request.Form属性中可用。您可以像这样枚举它们:

foreach (var name in Request.Form.AllKeys)
{
    System.Diagnostics.Debug.WriteLine("Form Name: " + name + " Form Value: " + Request.Form[key]);
}

If the value is checked, it will be in there.

如果检查了该值,它将在那里。

There may be lots of form values, so you'll need to filter them to get only what you want. I suggest you prefix your name with a particular string. Ex:

可能有很多表单值,因此您需要过滤它们以获得您想要的内容。我建议你在名字前加上一个特定的字符串。例如:

results.Append("<td><input id='" + RosteredCareID + "' type='checkbox' name='RosterCareCB_" + RosteredCareID  + "' unchecked /></td>");

Then on the server side, filter on that prefix.

然后在服务器端,过滤该前缀。

var checkedIds = new List<string>();
foreach (var name in Request.Form.AllKeys)
{
    if(name.StartsWith("RosterCareCB_") && Request.Form[name] == "on")
    {
        checkedIds.Add(name);
    }
}