获取不到table,jquery给table动态添加一行数据
<table width="100%" id="diarylist" border="0" cellspacing="1"cellpadding="0">
<thead></thead>
<tbody id="listtable" runat="server">
</tbody>
</table>
这是前台写的,在后台动态添加了数据
public static int i = 0;
public static string currenttime = DateTime.Now.ToString();
protected void Page_Load(object sender, EventArgs e)
{
//string str = Request.QueryString["UserID"].ToString();
string cmd = "select top 5 * from WorkDiary where datediff(day, Work_StartTime,GETDATE())=0 order by Work_ID desc";
DataTable dt = new DataTable();
dt = getDataTable(cmd);
string str = "";
foreach (DataRow ds in dt.Rows)
{
str += "<tr>" + "<td style="width: 50%; padding-left: 15px;">" + ds["Work_Title"]
+ "</td><td style="width: 50%; padding-left: 100px;">" + ds["Work_Description"] + "</td></tr>";
i = i + 1;
}
listtable.InnerHtml = str;
//LogListView.DataSource = getDataTable(cmd);
//LogListView.DataBind();
}
jquery动态添加
if (i < 5) {
$("#<%=diarylist.ClientID %> tbody").prepend("<tr><td style='width: 50%; padding-left: 15px;'>" + subject + "</td><td style='width: 50%; padding-left: 15px;'>" + description + "</td></tr>");
i++;
}
else {
$("#<%=diarylist.ClientID %> tbody").prepend("<tr><td style='width: 50%; padding-left: 15px;'>" + subject + "</td><td style='width: 50%; padding-left: 15px;'>" + description + "</td></tr>");
}
报错 当前上下文不存在diarylist
[解决办法]
哥 <%=diarylist.ClientID %> 这个是获取服务器控件才这么做的,你现在这个 <table width="100%" id="diarylist" border="0" cellspacing="1"cellpadding="0">table,就直接#diarylist不就行了