由於是項目的一部分,我也就只一部分代碼共大家參考:
<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);