<%@ Page Language="C#" %>
<%@ Import namespace="System" %>
<%@ Import namespace="System.Data" %>
<%@ Import namespace="System.IO" %>
<script language="C#" runat="server">
//用数组模拟数据库
	String [] arrCol = {"customerid", "companyname", "contactname", "country", "city", "address", "phone"};
	String [] arrCustomer = {
"ALFKI	Alfreds Futterkiste	Maria Anders	Germany	Berlin	Obere Str. 57	030-0074321",
"ANATR	Ana Trujillo Emparedados y helados	Ana Trujillo	Mexico	Mexico D.F.	Avda. de la Constitucion 2222	(5) 555-4729",
"ANTON	Antonio Moreno Taquería	Antonio Moreno	Mexico	Mexico D.F.	Mataderos  2312	(5) 555-3932",
"AROUT	Around the Horn	Thomas Hardy	UK	London	120 Hanover Sq.	(171) 555-7788",
"BERGS	Berglunds snabbkp	Christina Berglund	Sweden	Lule	Berguvsvgen  8	0921-12 34 65",
...(略)
};

public void Page_Load(Object sender, EventArgs e) {
 //取得搜索的子串
 String substr = Request.Params["substr"];
 if(substr==null) return;
 
 //同时也允许分页
 int nRows = -1;
 int nStart = -1;
 String Rows = Request.QueryString["Rows"];
 String StartRow = Request.QueryString["startRow"];
 if(Rows!=null) nRows = Int32.Parse(Rows);
 if(StartRow!=null) nStart = Int32.Parse(StartRow);
 if(nStart == -1) {
   String Page = Request.QueryString["page"];
   if(Page!=null) {
     int nPage = Int32.Parse(Page);
     if(nPage >= 0) nStart = nPage * nRows;
   }
 }
 
 //不分页的==================
 if(nStart == -1) {
  for(int i=0; i<arrCustomer.Length; i++) {
   String s = arrCustomer[i];
   if(s.IndexOf(substr, StringComparison.CurrentCultureIgnoreCase)==-1) continue;
   Response.Write(s + "\r\n");
  }
  Response.ContentType = "text/plain";
  return;
 }
 
 //分页的==================
 //总行数
 int TotalRows = 0;
 for(int i=0; i<arrCustomer.Length; i++) {
  String s = arrCustomer[i];
  if(s.IndexOf(substr, StringComparison.CurrentCultureIgnoreCase)==-1) continue;
  TotalRows++;
 }
 Response.Write("<root>");
 Response.Write("<totalrows>" +TotalRows.ToString()+ "</totalrows>");
 Response.Write("<table>");
 
 int count=0;
 int nRow=0;
 for(int i=0; i<arrCustomer.Length; i++) {
  String s = arrCustomer[i];
  if(s.IndexOf(substr, StringComparison.CurrentCultureIgnoreCase)==-1) continue;
  nRow++;
  if(nRow < nStart) continue;
 
  Response.Write("<row>");
  String [] arr = s.Split(new Char[] { '\t' });
  for(int j=0; j<7; j++) {
   Response.Write("<" + arrCol[j] + ">");
   String s1="";
   if(j < arr.Length) s1 = arr[j];
   s1 = s1.Replace("&", "&amp;");
   Response.Write(s1);
   Response.Write("</" + arrCol[j] + ">");
  }
  Response.Write("</row>");
 
  count++;
  if(count >= nRows) break;
 }
 
 Response.Write("</table>");
 Response.Write("</root>");
 Response.ContentType ="text/xml";
 Response.CacheControl="no-cache";
 Response.End();
}
</script>