我正在研究.net项目。我有一个DataTable(https://datatables.net/),可以从数据库中加载数据。该数据表看起来像。我还弹出了一个表单,该表单会将数据添加到我的数据表中。我的表单看起来像。我已经标记了添加项目按钮和搜索栏。我的问题是,当我在表搜索和分页栏中添加新值时,我所标记的内容丢失了。这是我的表定义。
<asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional">
<ContentTemplate>
<table id="example" class="table table-striped table-bordered nowrap" style="width: 100%">
<thead>
<tr>
<%-- <th>Name</th>
<th>Position</th>
<th>Office</th>
<th>Age</th>
<th>Start date</th>
<th>Salary</th>--%>
<th>Code</th>
<th>Item Name</th>
<th>Specification</th>
<th>Unit</th>
<th>Qnty</th>
<th>Rate</th>
<th>Amount</th>
<%--<th style="width: 60px">Edit</th>
<th style="width: 60px">Delete</th>--%>
</tr>
</thead>
<tbody>
<asp:Literal ID="Literal1" runat="server"></asp:Literal>
<%-- <tr>
<td>Tiger Nixon</td>
<td>System Architect</td>
<td>Edinburgh</td>
<td>61</td>
<td>2011/04/25</td>
<td>$320,800</td>
</tr>
<tr>
<td>Garrett Winters</td>
<td>Accountant</td>
<td>Tokyo</td>
<td>63</td>
<td>2011/07/25</td>
<td>$170,750</td>
</tr>
<tr>
<td>Ashton Cox</td>
<td>Junior Technical Author</td>
<td>San Francisco</td>
<td>66</td>
<td>2009/01/12</td>
<td>$320,800</td>
</tr>
<tr>
<td>Cedric Kelly</td>
<td>Senior Javascript Developer</td>
<td>Edinburgh</td>
<td>22</td>
<td>2012/03/29</td>
<td>$320,800</td>
</tr>
<tr>
<td>Airi Satou</td>
<td>Accountant</td>
<td>Tokyo</td>
<td>33</td>
<td>2008/11/28</td>
<td>$320,800</td>
</tr>
<tr>
<td>Brielle Williamson</td>
<td>Integration Specialist</td>
<td>New York</td>
<td>61</td>
<td>2012/12/02</td>
<td>$320,800</td>
</tr>--%>
</tbody>
<tfoot>
<tr>
<th>Code</th>
<th>Item Name</th>
<th>Specification</th>
<th>Unit</th>
<th>Qnty</th>
<th>Rate</th>
<th>Amount</th>
<%-- <th style="width: 60px">Edit</th>
<th style="width: 60px">Delete</th>--%>
</tr>
</tfoot>
</table>
</ContentTemplate>
</asp:UpdatePanel>
这是将数据添加到表的方式。
protected void btnItemAdd_Click(object sender, EventArgs e)
{
lblMsg.Text = "";
decimal amt = -1;
if (!decimal.TryParse(txtQty.Text, out amt))
{
lblMsg.Text = @"Invalid Quentity";
txtQty.Focus();
return;
}
if (!decimal.TryParse(txtrate.Text, out amt))
{
lblMsg.Text = @"Invalid Rate";
txtrate.Focus();
return;
}
add_reqItemList(ddlItem.SelectedValue.ToString(), ddlItem.Text, txtItemSpecific.Text.Trim(), txtUnit.Text.Trim(), txtQty.Text, txtrate.Text, txtamt.Text);
}
private void grdItem_DataBind()
{
DataTable tbl1 = (DataTable)ViewState["REQITEMLIST"];
string _itemCode, _itemName, _temDesc, _itemUnit, _itemQty, _itemRate, _itemAmt;
for (int rowcnt = 0; rowcnt < tbl1.Rows.Count; rowcnt++)
{
_itemCode = tbl1.Rows[rowcnt]["ItmCode"].ToString();
_itemName = tbl1.Rows[rowcnt]["ItmName"].ToString();
_temDesc = tbl1.Rows[rowcnt]["ItmDesc"].ToString();
_itemUnit = tbl1.Rows[rowcnt]["ItmUnit"].ToString();
_itemQty = tbl1.Rows[rowcnt]["ItmQty"].ToString();
_itemRate = tbl1.Rows[rowcnt]["ItmRate"].ToString();
_itemAmt = tbl1.Rows[rowcnt]["TTMAMNT"].ToString();
if (rowcnt == 0)
{
mnu = "<tr>";
}
else
{
mnu = mnu + "<tr>";
}
mnu = mnu + "<td><a href=" + x + "#ItemAddEdit" + x + " name=" + x + "abc" + x + " onclick=" + x
+ "openModal('" + _itemCode + "','" + _itemName + "','" + _temDesc + "','" + _itemUnit + "','" + _itemQty + "','" + _itemRate + "','" + _itemAmt + "');" + x + ">"
+ _itemCode + "</a></td>";
mnu = mnu + "<td>" + _itemName + "</td>";
mnu = mnu + "<td>" + _temDesc + "</td>";
mnu = mnu + "<td>" + _itemUnit + "</td>";
mnu = mnu + "<td>" + _itemQty + "</td>";
mnu = mnu + "<td>" + _itemRate + "</td>";
mnu = mnu + "<td>" + _itemAmt + "</td>";
mnu = mnu + "</tr>";
};
Literal1.Text = mnu;
}
private void add_reqItemList(string _ITMCODE, string _ITMNAME, string _ITMDESC, string _ITMUNIT, string _ITMQTY, string _ITMRATE, string _TTMAMNT)
{
dt = new DataTable();
dt = (DataTable)ViewState["REQITEMLIST"];
DataRow row1;
row1 = dt.NewRow();
row1["ITMCODE"] = _ITMCODE;
row1["ITMNAME"] = _ITMNAME;
row1["ITMDESC"] = _ITMDESC;
row1["ITMUNIT"] = _ITMUNIT;
row1["ITMQTY"] = _ITMQTY;
row1["ITMRATE"] = _ITMRATE;
row1["TTMAMNT"] = _TTMAMNT;
dt.Rows.Add(row1);
ViewState["REQITEMLIST"] = dt;
grdItem_DataBind();
UpdatePanel1.Update();
}
请帮助解决该问题
答案 0 :(得分:0)
向DataTable添加数据后,您可能需要重新初始化表:
if ($.fn.DataTable.isDataTable("#mytable")) {
$('#mytable').DataTable().clear().destroy();
}
$("#mytable").dataTable();