javascript - 如何将通过 Javascript 添加的表行发布到 ASP.NET 中的服务器?

标签 javascript jquery asp.net vb.net webforms

这个问题之前有人问过:Access <asp:table> table rows added by javascript in asp.net webform .为重复的问题道歉,但我真的很想解释为什么会这样。这可能是因为我不了解浏览器如何处理提交给服务器的 HTML 表格。

如果我在 aspx 页面上有一个 表或一个 控件,并且我使用 JQuery/Javascript 在客户端向其添加行,为什么我不能在回发中包含这些添加的行到服务器?

我一直在努力让它发挥作用,但根据对上一个问题的回答,我似乎做不到。但是有人可以解释为什么会这样吗?表本身可以在回发中返回,但唯一存在的行是最初发送到浏览器时作为表一部分的行 - 它不包括浏览器添加的行。

我本以为有一种方法可以在回发中包含这些新行,就像任何客户端用户输入一样?

最佳答案

你可以这样做:

// Get the data from your table into an array
var tableData = [];
$("table tr.some-class").each(function () {
  var row = [];
  $(this).find("td").each(function () {
    row.push(this.innerHTML);
  });
  tableData.push(row);
});

// Make your form
var form = $("<form>").attr("action", "some/path/on/server")
                      .attr("method", "post");

// Make a form field with your tableData (JSON serialized in this case)
var tableInput = $("<input>").attr("type", "hidden")
                             .attr("value", JSON.stringify(tableData));

// Some browsers require the form to be in the dom before it'll submit.
$(document.body).append(form);
// Add the field to the form and submit
form.append(tableInput).submit();

这是 this answer 的基本实现.

关于javascript - 如何将通过 Javascript 添加的表行发布到 ASP.NET 中的服务器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7638272/

相关文章:

javascript - 无法呈现在运行时动态设置的集合

javascript - org.scalajs.dom.ext.ajax.post 上的错误代码 405

c# - 如何调试此 Web 服务?

javascript - 在数组内部映射数组

javascript - ajax 防止在 mouseenter 上重复发布

javascript - 使用 jQuery 在计时器上使用 ajax 更新 google 图表

javascript - 给h1的最后两个字母涂其他颜色

javascript - 是否可以针对特定的 jQuery Ajax 调用禁用 Turbolinks 以防止页面刷新和滚动?

c# - 如何使用 ASP.NET 访问 Outlook 邮件?

c# - ASP.NET MVC 6 - 将 NOT NULL 分配给 Entity Framework 7 中的主键