Reputation: 433
I have a html table that I'm running at the server level. However, I'm having trouble accessing the data that it is being populated with.
I'm populating the table using javascript but I need to access the data using vb.net.
I'm using jquery to get the information to the page by using the .html
method.
Here is my 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);
}
I am passing the information from vb.net using an `ArrayList.
Here is my 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>
And here is my 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
Upvotes: 4
Views: 2035
Reputation: 654
dynamic rows created in JS will not be available on server after postback as it is not in the viewstate to workaround this you may do the following
you can add hidden input field to the page like
<input type="hidden" id="hiddenTableData" name="hiddenTableData" value="" />
then in your script add the data you need to the hidden input field,
$('#hiddenTableData').val(JSON.stringify(ar));
then on server on page post back, get the value of the form hidden field
String tableData= Request.Form["hiddenTableData"];
parse the hidden field value, populate your table and process it
Upvotes: 2
Reputation: 7462
I have tried the same example in my end, and faced the same issue that , generated TR are not accessible in code behind. This is because, they are added using dynamic JS, and those are not put into VIEWSTATE, as you now, ASP control value are available in code behind on POSTBACK only if those are in VIEWSTATE. In this case it was not, so does the debugger says that projGrid.Rows.Count = 0. To make it work, you need to put changed/dynamic JS added TR into VIEWSTATE, but again those are encoded format, i afraid, how to do that.
But if you put any static TR inside table, then definitely those are accessible, I have verified that in debugger, it says projectGrid.Rows.Count = 1, if there is only one static TR inside table. And again because, that static TR is in VIEWSTATE only.
So to conclude, in my opinion, it is better to handle in JS side, if you are going to save those value to DB , then make AJAX call and save, and pass data in JSON format. Or use a hidden field, and save those data in JSON format, and after post, get those in code behind, read JSON format data, and operate.
Upvotes: 1
Reputation: 131
if you are using JavaScript Method
or service to fill any controls then you will not get data at code page. but you can pass that array to code page and then can get easily data as you want .
For this you should create a class same as array(in web page you are using) and a web-method which accepts this(array) class type.
Upvotes: 1