【发布时间】:2018-05-19 14:39:16
【问题描述】:
我正在尝试从我的 SQL 数据库中提取数据并将其显示在 Jquery DataTable 中。我可以使用 Gridview 来做到这一点,但现在想更改为 Jquery DataTable 以获得更多功能。似乎数据正在我的 BindGrid() 函数中收集,但它实际上不会显示在网页的表格中,标题显示但它们下方没有信息。
另外,这是我现在的所有代码:
HTML:
<div style="margin-top:30px;">
<table class="table table-striped table-bordered" style="font-family: serif; border:1px;" id="netEventTable" clientIdMode="static">
<thead>
<tr>
<th>ID</th>
<th>Username</th>
<th>Type</th>
<th>Subject Line</th>
<th>Start Time</th>
<th>Estimated Time of Resolution</th>
</tr>
</thead>
<tbody id="netEventTableBody" runat="server">
</tbody>
</table>
</div>
查询:
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.6/jquery.min.js" type="text/javascript"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.12.0/jquery-ui.min.js" type="text/javascript"></script>
<script src="https://cdn.datatables.net/1.10.10/js/jquery.dataTables.min.js" type="text/javascript"></script>
<link href="" rel="stylesheet" />
<script type="text/javascript">
$(document).ready(function () {
$('#netEventTable').dataTable({
"bLengthChange": true,
"paging": true,
"sPaginationType": "full_numbers",
"jQueryUI": true,
});
});
</script>
C#(代码隐藏):
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
this.BindGrid();
}
}
private void BindGrid()
{
string strSQLConn = ConfigurationManager.ConnectionStrings["DatabaseConnectionString"].ConnectionString;
try
{
// Jquery DataTable
SqlConnection con = new SqlConnection(strSQLConn);
con.Open();
SqlCommand cmd = new SqlCommand("getNetEvents", con);
SqlDataReader user = cmd.ExecuteReader();
String UnreadText = "";
Int32 i = 0;
while (user.Read())
{
UnreadText += "<tr>";
UnreadText += "<td class=\'center'>" + user["ID"] + "</td>";
UnreadText += "<td class=\'center'>" + user["Name"] + "</td>";
UnreadText += "<td class=\'center'>" + user["Type"] + "</td>";
UnreadText += "<td class=\'center'>" + user["SubjectLine"] + "</td>";
UnreadText += "<td class=\'center'>" + user["StartTime"] + "</td>";
UnreadText += "<td class=\'center'>" + user["EndTime"] + "</td>";
UnreadText += "</tr>";
i++;
}
con.Close();
}
catch (Exception ex)
{
Console.WriteLine("ERROR: " + ex.ToString());
Helpers.ApplicationLogHelper.createApplicationLogError("ERROR", ex.ToString());
return;
}
}
如何从我的 SQL 服务器获取数据以显示在 Jquery DataTable 中?
【问题讨论】:
-
将
UnreadText包含的任何内容(看起来)呈现到前端/HTML/aspx 的代码在哪里? -
我不认为我有任何渲染它的东西,我应该怎么做?
标签: c# jquery sql asp.net datatables