由于是项目的一部分,我也就只截图一部分代码共大家参考:
<table width="100%" border="0" cellspacing="0" cellpadding="5"> <tr> <td id="LatestNews" height="330" align="center" valign="top" class="font14" style="border: #7baed9 solid 1px; border-top: none; padding: 10px;"> </td> </tr> </table>
主要是想通过jQuery的ajax获取数据后显示在ID号为"LatestNews"td里。下面是ajax代码:
<script type="text/javascript"> $(function() { $.ajax({ type: "POST", url: "ceo2012/LatestNews.ashx", contentType: "application/json;charset=utf-8", dataType: "json", success: function(data) { $("#LatestNews").html(data); }, error: function(error) { $("#LatestNews").html("尚未发布任何信息!"); } }); }); </script>
下面是ceo2012文件夹下的LatestNews.ashx的一般处理文件里的一个方法:
/// <summary> /// 媒体最新报道 /// </summary> /// <param name="context"></param> public void ProcessRequest(HttpContext context) { context.Response.ContentType = "text/plain"; JavaScriptSerializer javaScriptSerializer = new JavaScriptSerializer(); String strHtml = "<ul style='text-align:left'>"; DataTable dt = new DataTable(); NewsRule newsRule = new NewsRule(); dt = newsRule.GetNewsByTID(640, 15, "SortID desc"); if (dt.Rows.Count == 0) { context.Response.Write("尚未发布任何信息!"); return; } for (int i = 0; i < dt.Rows.Count; i++) { if (!String.IsNullOrEmpty(dt.Rows[i]["aUrl"].ToString())) { strHtml += "<li><a href='" + dt.Rows[i]["aUrl"].ToString() + "' title='" + dt.Rows[i]["title"].ToString() + "'target=\"_blank\">" + dt.Rows[i]["title"].ToString() + "</a>"; } else { strHtml += "<li><a href=\"http://www.sino-manager.com/" + ReturnDate(DateTime.Parse(dt.Rows[i]["PubTime"].ToString()).ToShortDateString()) + "_" + dt.Rows[i]["PostID"].ToString() + ".html\" title=\"" + dt.Rows[i]["title"].ToString() + "\" target=\"_blank\">" + dt.Rows[i]["title"].ToString() + "</a></li>"; } } strHtml += "</ul>"; String content = javaScriptSerializer.Serialize(strHtml); context.Response.Write(content);