简体   繁体   English

如何在HTML中向现有表添加新行

[英]How to add a new row to existing table in HTML

Here is my ASP code to display a database table in the form of HTML table 这是我的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>");
}

Here's my insert code: 这是我的插入代码:

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();
}

It's working perfectly. 运行良好。 But I don't want to read whole table again when I perform insert operation. 但是我不想在执行插入操作时再次读取整个表。 I just want to append the row to HTML table which is defined in display method when cmd.ExecuteNonQuery returns 1 (To make sure that the database table got updated). 我只想在cmd.ExecuteNonQuery返回1时将行追加到显示方法中定义的HTML表中(以确保数据库表已更新)。 How? 怎么样?

in your ajax succeess callback append new row to table 在您的ajax成功回调中将新行追加到表

succcess:function(data)
{
    var newRow= "";                   
    newRow= newRow+ "<tr>";
    newRow= newRow+ "<td>" + studentId + "</td>";
    newRow= newRow+ "<td>" + studentName + "</td>";      
    newRow= newRow+ "</tr>";                        
    $('.yourtable').append(newRow);
}

Create HTML table as server side control using runat='server'. 使用runat ='server'创建HTML表作为服务器端控件。 So your table creation part will be as below; 因此,您的表创建部分如下所示;

**Response.Write("<Table runat='server' id='tblStudents' cellpadding = 5>");**

After Saving, you can access this control from code behind and add rows. 保存后,您可以从后面的代码访问此控件并添加行。 Add below code after successful insertion; 成功插入后添加以下代码;

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);

I would suggest this way using jquery's append . 我建议使用jquery的append这种方式。 It will append row after finding last child within the tbody of your table : tabletbody中找到最后一个孩子后,它将追加行:

$('#yourTableID > tbody:last-child').append('<tr><td>Some Data</td>...</tr>');

Note: Table should have at least one row for this to work which you can easily do by counting the rows within your table using jquery. 注意:表格至少应具有row ,您可以使用jquery对表格中的行进行计数,从而轻松地做到这一点。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM