1,前台:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="test.aspx.cs" Inherits="test" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "">
<html xmlns="">
<head runat="server"> <title>无标题页</title></head><body> <form id="form1" runat="server"> <div> <asp:TextBox ID="show" runat="server" TextMode="MultiLine" style=" height:600px; width:600px;"></asp:TextBox> </div> </form></body></html>2,后台:using System;
using System.Collections;using System.Configuration;using System.Data;using System.Linq;using System.Web;using System.Web.Security;using System.Web.UI;using System.Web.UI.HtmlControls;using System.Web.UI.WebControls;using System.Web.UI.WebControls.WebParts;using System.Xml.Linq;using System.Text;public partial class test : System.Web.UI.Page
{ protected void Page_Load(object sender, EventArgs e) { if(!IsPostBack) { show.Text = server_Side_Processing(20,1); } } /// <summary> ///服务器端处理程序到数据库查询数据并生成xml档返回 /// </summary> public string server_Side_Processing(int pageSize, int currentPage) { StringBuilder resultXML = new StringBuilder(); string str_xml; DataSet ds; DataAccess da; int i; resultXML.Append("<?xml version='1.0' encoding='gb2312'?>"); resultXML.Append("<ajax-response>\n"); resultXML.Append("<root>\n");try
{ if (currentPage == 1) { str_xml = "select top " + pageSize + " * from tbMicroblog order by MicroblogID desc"; } else { str_xml = "select top " + pageSize + " * from tbMicroblog MicroblogID not in (select top " + pageSize * (currentPage - 1) + " * from tbMicroblog order by MicroblogID desc) where order by MicroblogID desc"; } da = new DataAccess(); ds = da.GetDataSetResult(str_xml, "xml_table");if (ds != null)
{for (i = 0; i <pageSize; i++)
{ if (ds.Tables[0].Rows[i]==null) { break; }resultXML.Append("<data>\n");
resultXML.Append("\t<MicroblogID>" + ds.Tables[0].Rows[i]["MicroblogID"].ToString() + "</MicroblogID>\n");if (ds.Tables[0].Rows[i]["UserID"].ToString().Trim() != "")
{ resultXML.Append("\t<UserID>" + HttpUtility.UrlEncodeUnicode(ds.Tables[0].Rows[i]["UserID"].ToString().Trim()) + "</UserID>\n"); } else { resultXML.Append("\t<UserID>#</UserID>\n"); }if (ds.Tables[0].Rows[i]["Content"].ToString().Trim() != "")
{ resultXML.Append("\t<Content>" + HttpUtility.UrlEncodeUnicode(ds.Tables[0].Rows[i]["Content"].ToString().Trim()) + "</Content>\n"); } else { resultXML.Append("\t<Content>#</Content>\n"); }if (ds.Tables[0].Rows[i]["Pubdate"].ToString().Trim() != "")
{ resultXML.Append("\t<Pubdate>" + HttpUtility.UrlEncodeUnicode(ds.Tables[0].Rows[i]["Pubdate"].ToString().Trim()) + "</Pubdate>\n"); } else { resultXML.Append("\t<Pubdate>#</Pubdate>\n"); } resultXML.Append("</data>\n"); } } else { //resultXML.Append("<?xml version='1.0' encoding='gb2312'?>"); //resultXML.Append("<ajax-response>\n"); //resultXML.Append("<root>\n"); resultXML.Append("<data>\n"); resultXML.Append("\t<nodata>" + "No Data !" + "</nodata>\n"); resultXML.Append("</data>\n"); }}
catch (Exception e) { Console.WriteLine("{0} Exception caught.", e); }resultXML.Append("</root>\n");
resultXML.Append("</ajax-response>");return resultXML.ToString();
}}
思路分析:if (ds.Tables[0].Rows[i]==null)
{ break; }这里是关键.
以pageSize个数据单元作为一页(数据段),当数据段的数据单元数目<pageSize时就是最后一页了。