[英]Getting Data in vb.net from dynamic htmlTable
我有一个html表,我在服务器级别运行。 但是,我无法访问正在填充的数据。
我正在使用javascript填充表,但我需要使用vb.net访问数据。
我正在使用jquery通过使用.html
方法将信息提供给页面。
这是我的javascript,
function ShowProj(strId, qno,ar) {
$('#hiddenQno').val(qno);
$('#qnoLbl').val(qno);
var output = '';
output = "<tr><th>Product</th>";
output += "<th>Bond</th>";
output += "<th>Cut</th>";
output += "<th>Pack</th>";
output += "<th>Delivery date</th></tr>";
for (i = 0; i < ar.length; i++) {
output += "<tr>";
output += "<td style='width: 40%;'>" + ar[i][0] + "</td>";
output += "<td style='width: 10%; text-align: center;'><input type='checkbox' " + (ar[i][1] == 1 ? 'checked' : '') + " /></td>";
output += "<td style='width: 10%; text-align: center;'><input type='checkbox' " + (ar[i][2] == 1 ? 'checked' : '') + " /></td>";
output += "<td style='width: 10%; text-align: center;'><input type='checkbox' " + (ar[i][3] == 1 ? 'checked' : '') + " /></td>";
output += "<td style='width: 30%;'><input type='text' value='" + ar[i][4] + "'/><input type='hidden' value='" + ar[i][5] + "' /></td>";
output += "</tr>"
}
$('#projGrid').html(output);
}
我使用`ArrayList从vb.net传递信息。
这是我的HTML,
<div class="modalPopup" id="projPopup" style="width: 40%;">
<table style="width:100%;">
<tr>
<td colspan="2">
<table id="projGrid" runat="server" style="width: 100%;border: 1px solid black; font-size: 16;">
</table>
</td>
</tr>
<tr>
<td>
</td>
<td>
</td>
</tr>
<tr>
<td>
<asp:Button ID="projOk" runat="server" Text="Update" /></td>
<td>
<asp:Button ID="projCncl" runat="server" Text="Cancel" />
</td>
</tr>
</table>
</div>
这是我的vb.net。
For Each row As HtmlTableRow In projGrid.Rows
'Dim chkBond As HtmlInputCheckBox = row.Cells(1).Controls(0)
Dim str As String
str = row.Cells(0).InnerText
Next
在回发后,在JS中创建的动态行将无法在服务器上使用,因为它不在视图状态中解决此问题,您可以执行以下操作
你可以添加隐藏的输入字段到页面
<input type="hidden" id="hiddenTableData" name="hiddenTableData" value="" />
然后在您的脚本中将您需要的数据添加到隐藏的输入字段,
$('#hiddenTableData').val(JSON.stringify(ar));
然后在页面上的服务器上发回,获取表单隐藏字段的值
String tableData= Request.Form["hiddenTableData"];
解析隐藏的字段值,填充表并处理它
如果您使用JavaScript Method
或服务来填充任何控件,那么您将无法在代码页获取数据。 但是您可以将该数组传递给代码页,然后可以根据需要轻松获取数据。
为此,您应该创建一个与数组相同的类(在您正在使用的网页中)和一个接受此(数组)类类型的Web方法。
我在最后尝试了相同的例子,并面临同样的问题,即生成的TR无法在后面的代码中访问。 这是因为,它们是使用动态JS添加的,并且这些不会被放入VIEWSTATE,因为现在,只有在VIEWSTATE中,ASP控件值才能在POSTBACK后面的代码中使用。 在这种情况下它不是,调试器也说projGrid.Rows.Count = 0.为了使它工作,你需要把改变/动态JS添加TR到VIEWSTATE,但再次那些是编码格式,我害怕,如何要做到这一点。
但是如果你在表中放置任何静态TR,那么肯定是可访问的,我已经验证在调试器中,它表示projectGrid.Rows.Count = 1,如果表中只有一个静态TR。 再次因为,静态TR仅在VIEWSTATE中。
总而言之,在我看来,最好在JS方面处理,如果要将这些值保存到DB,然后进行AJAX调用并保存,并以JSON格式传递数据。 或者使用隐藏字段,并以JSON格式保存这些数据,并在发布后,获取代码隐藏,读取JSON格式数据,并进行操作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.