这是我的ASP代码,以HTML表格的形式显示数据库表
if (operation == "display"){
MySqlDataAdapter da = new MySqlDataAdapter("select StudentId, StudentName from tblStudent where StudentStatus = 'a'", con);
DataTable dt = new DataTable();
da.Fill(dt);
Response.Write("<Table cellpadding = 5>");
Response.Write("<tr>");
Response.Write("<th>");
Response.Write("Student Id");
Response.Write("</th>");
Response.Write("<th>");
Response.Write("Student Name");
Response.Write("</th>");
Response.Write("</tr>");
foreach (DataRow dr in dt.Rows){
Response.Write("<tr>");
Response.Write("<td>");
Response.Write(dr["StudentId"].ToString());
Response.Write("</td>");
Response.Write("<td>");
Response.Write(dr["studentName"].ToString());
Response.Write("</td>");
Response.Write("</tr>");
}
Response.Write("</Table>");
}
这是我的插入代码:
if (operation == "insert"){
string sid, sname;
sid = Request.QueryString["StudentId"].ToString();
sname = Request.QueryString["StudentName"].ToString();
//MySqlDataAdapter da = new MySqlDataAdapter("insert into tblStudent (StudentId, StudentName) values (sid, sname)", con);
con.Open();
MySqlCommand cmd = con.CreateCommand();
cmd.CommandType = CommandType.Text;
cmd.CommandText = "insert into tblStudent (StudentId, StudentName) values('" + sid.ToString() + "', '" + sname.ToString() + "')";
cmd.ExecuteNonQuery();
con.Close();
}
它工作得很好。但是当我执行插入操作时,我不想再读取整个表格。我只想将行追加到HTML表中,当cmd.ExecuteNonQuery返回1时(在确保数据库表更新时),该表在display方法中定义。怎么样?
答案 0 :(得分:0)
succcess:function(data)
{
var newRow= "";
newRow= newRow+ "<tr>";
newRow= newRow+ "<td>" + studentId + "</td>";
newRow= newRow+ "<td>" + studentName + "</td>";
newRow= newRow+ "</tr>";
$('.yourtable').append(newRow);
}
答案 1 :(得分:0)
使用runat =&#39; server&#39;创建HTML表作为服务器端控件。 因此,您的表创建部分将如下所示;
**Response.Write("<Table runat='server' id='tblStudents' cellpadding = 5>");**
保存后,您可以从后面的代码访问此控件并添加行。 成功插入后添加以下代码;
var row= new System.Web.UI.HtmlControls.HtmlTableRow();
var cellSID = new System.Web.UI.HtmlControls.HtmlTableCell();
cellSID.InnerText = sid;
row.Cells.Add(cellSID);
var cellSName = new System.Web.UI.HtmlControls.HtmlTableCell();
cellSID.InnerText = sname;
row.Cells.Add(cellSName);
tblStudents.Rows.Insert(0, row);
答案 2 :(得分:0)
我会建议使用jquery&#39; s append
。它会在找到tbody
的{{1}}中的最后一个孩子后追加行:
table
注意:表格至少应有一个$('#yourTableID > tbody:last-child').append('<tr><td>Some Data</td>...</tr>');
,您可以通过使用jquery计算表格中的行来轻松完成此操作。