asp.NetでJquery+Ajax+Jsonを用いてリフレッシュページのないインスタンスコードを実現
Jquery+Ajax+Json
<br> var pageIndex =0;
<br> var pageSize =10;
<br> $(function () {<br> InitTable(0);
<br>
<br> $("#Pagination").pagination(<%=pageCount %>,{<br> callback:PageCallback,<br> prev_text:' ',
<br> next_text:' ',
<br> items_per_page:pageSize,
<br> num_display_entries:6,
<br> current_page:pageIndex,
<br> num_edge_entries:2
<br> });<p></p><p>
<p>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Text;
using System.Net;
using System.IO;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class AjaxJson : System.Web.UI.Page
{
public string pageCount = string.Empty; //
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
string url = "/SupplyAJAX.aspx";
string strResult = GetRequestJsonString(url, "type=getcount");
pageCount = strResult.ToString();
}
}
#region ashx
///
/// ashx
///
///
///
///
public static string GetRequestJsonString(string relativePath, string data)
{
string requestUrl = GetRequestUrl(relativePath, data);
try
{
WebRequest request = WebRequest.Create(requestUrl);
request.Method = "GET";
StreamReader jsonStream = new StreamReader(request.GetResponse().GetResponseStream());
string jsonObject = jsonStream.ReadToEnd();
return jsonObject;
}
catch
{
return string.Empty;
}
}
public static string GetRequestUrl(string relativePath, string data)
{
string absolutePath = HttpContext.Current.Request.Url.AbsoluteUri;
string hostNameAndPort = HttpContext.Current.Request.Url.Authority;
string applicationDir = HttpContext.Current.Request.ApplicationPath;
StringBuilder sbRequestUrl = new StringBuilder();
sbRequestUrl.Append(absolutePath.Substring(0, absolutePath.IndexOf(hostNameAndPort)));
sbRequestUrl.Append(hostNameAndPort);
sbRequestUrl.Append(applicationDir);
sbRequestUrl.Append(relativePath);
if (!string.IsNullOrEmpty(data))
{
sbRequestUrl.Append("?");
sbRequestUrl.Append(data);
}
return sbRequestUrl.ToString();
}
#endregion
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Web.UI;
using System.Web.UI.WebControls;
//
using System.Web.Script.Serialization;
using System.Text;
public partial class SupplyAJAX : System.Web.UI.Page
{
protected static List StudentList = new List();
protected static int RecordCount = 0;
protected static DataTable dt = CreateDT();
protected void Page_Load(object sender, EventArgs e)
{
switch (Request["type"])
{
case "show":
#region
//
int pageIndex;
int.TryParse(Request["pageIndex"], out pageIndex);
//
int PageSize = Convert.ToInt32(Request["pageSize"]);
if (pageIndex == 0)
{
pageIndex = 1;
}
#endregion
DataTable PagedDT = GetPagedTable(dt, pageIndex, PageSize);
List list = new List();
foreach (DataRow dr in PagedDT.Rows)
{
Student c = new Student();
c.Id = (Int32)dr["Id"];
c.Name = dr["Name"].ToString();
c.Sex = dr["Sex"].ToString();
list.Add(c);
}
string json = new JavaScriptSerializer().Serialize(list);// , error
StringBuilder Builder = new StringBuilder();
Builder.Append("{");
Builder.Append("\"recordcount\":" + RecordCount + ",");
Builder.Append("\"data\":");
Builder.Append(json);
Builder.Append("}");
Response.ContentType = "application/json";
Response.Write(Builder.ToString());
break;
case "getcount":
Response.Write(dt.Rows.Count);
break;
case "add":
break;
case "update":
break;
case "delete":
break;
}
Response.End();
}
#region
private static DataTable CreateDT()
{
DataTable dt = new DataTable();
dt.Columns.Add(new DataColumn("Id", typeof(int)) { DefaultValue = 0 });
dt.Columns.Add(new DataColumn("Name", typeof(string)) { DefaultValue = "1" });
dt.Columns.Add(new DataColumn("Sex", typeof(string)) { DefaultValue = " " });
for (int i = 1; i <= 1000; i++)
{
dt.Rows.Add(i, " " + i.ToString().PadLeft(4, '0'));
}
RecordCount = dt.Rows.Count;
return dt;
}
#endregion
///
/// DataTable , 1
///
///
///
///
///
public static DataTable GetPagedTable(DataTable dt, int PageIndex, int PageSize)
{
if (PageIndex == 0)
return dt;
DataTable newdt = dt.Copy();
newdt.Clear();
int rowbegin = (PageIndex - 1) * PageSize;
int rowend = PageIndex * PageSize;
if (rowbegin >= dt.Rows.Count)
return newdt;
if (rowend > dt.Rows.Count)
rowend = dt.Rows.Count;
for (int i = rowbegin; i <= rowend - 1; i++)
{
DataRow newdr = newdt.NewRow();
DataRow dr = dt.Rows[i];
foreach (DataColumn column in dt.Columns)
{
newdr[column.ColumnName] = dr[column.ColumnName];
}
newdt.Rows.Add(newdr);
}
return newdt;
}
///
///
///
///
///
///
public static int getPageCount(int sumCount, int pageSize)
{
int page = sumCount / pageSize;
if (sumCount % pageSize > 0)
{
page = page + 1;
}
return page;
}
public struct Student
{
public int Id;
public string Name;
public string Sex;
}
}