C# 使用<;%@在数据异步返回后分页GridView页面异步=";“真的”;
我正在阅读以下关于ASP.Net异步页面的博客C# 使用<;%@在数据异步返回后分页GridView页面异步=";“真的”;,c#,asp.net,C#,Asp.net,我正在阅读以下关于ASP.Net异步页面的博客 一个问题突然出现在我的脑海中,请考虑下面的情景: 假设一个异步页面 该页注册异步操作以从数据库检索数据,以便立即释放ASP.Net工作线程以增加可伸缩性 页面将分页信息传递给这些操作,以便在数据库服务器上分页 操作完成,并在新线程上调用正确的委托。(不使用ASP.Net线程池中的线程) 数据返回到页面,并可以绑定到页面上的GridView控件\u PreRendercomplete 此时,我已经准备好将页面上的数据绑定并显示回用户(只返回需
页面上的GridView
控件\u PreRendercomplete
GridView
控件,但我还没有弄清楚如何在我的GridView
上显示分页结果
我尝试使用以下代码:
protected override void OnPreRenderComplete(EventArgs e)
{
if (this.shouldRefresh)
{
var pagedSource = new PagedDataSource
{
DataSource = this.Jobs,
AllowPaging = true,
AllowCustomPaging = false,
AllowServerPaging = true,
PageSize = 3,
CurrentPageIndex = 0,
VirtualCount = 20
};
this.gv.DataSource = pagedSource;
this.gv.DataBind();
}
base.OnPreRenderComplete(e);
}
但是GridView
控件只是忽略了VirtualCount
属性,并且从未显示寻呼机,这就是我得到的:
ASPX
注:
- 我对jQuery异步发布到服务器以获取数据不感兴趣
实现MyClassResult
并从数据库服务器返回数据IAsyncResult
- 如果可能的话,我希望使用
ObjectDataSource
- 为了使分页工作,应该使用
。这样就可以告诉ObjectDatasource
总共有多少行GridView
- 我们需要一种方法,让我们的
在数据可用后访问我们获取的数据ObjectDataSource
为了解决2,我想出了一个主意。was定义了一个接口,
- 我想我有一些东西至少可以作为进一步探索的良好起点。我制作了一个示例(Futher down)来说明我的方法,该方法基于两个想法:
GridView
所在的页面可以实现该接口。然后,ObjectDataSource
可以使用一个类将获取数据的调用传递到页面本身。如果调用得太早,将返回空数据,但稍后将被实际数据替换
让我们看看一些代码。
这是我的aspx文件:
<%@ Page Title="" Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true"
CodeFile="GridViewTest.aspx.cs" Inherits="GridViewTest" %>
<asp:Content ID="Content1" ContentPlaceHolderID="HeadContent" runat="Server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="Server">
<asp:GridView ID="jobsGv" runat="server" AutoGenerateColumns="false" AllowPaging="true"
PageSize="13" OnPageIndexChanging="jobsGv_PageIndexChanging" DataSourceID="jobsDataSource">
<Columns>
<asp:TemplateField HeaderText="Job Id">
<ItemTemplate>
<asp:Literal ID="JobId" runat="server" Text='<%# Eval("JobId") %>'></asp:Literal>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Job description">
<ItemTemplate>
<asp:Literal ID="Description" runat="server" Text='<%# Eval("Description") %>'></asp:Literal>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Min level">
<ItemTemplate>
<asp:Literal ID="MinLvl" runat="server" Text='<%# Eval("MinLvl") %>'></asp:Literal>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<asp:ObjectDataSource ID="jobsDataSource" runat="server" TypeName="JobObjectDs" CacheDuration="0"
SelectMethod="GetJobs" EnablePaging="True" SelectCountMethod="GetTotalJobsCount">
</asp:ObjectDataSource>
<asp:Button ID="button" runat="server" OnClick="button_Click" Text="Test postback" />
</asp:Content>
以及背后的代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.UI.WebControls;
public partial class GridViewTest : System.Web.UI.Page, IJobDsPage
{
bool gridNeedsBinding = false;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
gridNeedsBinding = true;
}
}
protected void jobsGv_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
var gv = (GridView)sender;
newPageIndexForGv = e.NewPageIndex;
gridNeedsBinding = true;
}
private int newPageIndexForGv = 0;
protected void Page_PreRendercomplete(object sender, EventArgs e)
{
if (gridNeedsBinding)
{
// fetch data into this.jobs and this.totalJobsCount to simulate
// that data has just become available asynchronously
JobDal dal = new JobDal();
jobs = dal.GetJobs(jobsGv.PageSize, jobsGv.PageSize * newPageIndexForGv).ToList();
totalJobsCount = dal.GetTotalJobsCount();
//now that data is available, bind gridview
jobsGv.DataBind();
jobsGv.SetPageIndex(newPageIndexForGv);
}
}
#region JobDsPage Members
List<Job> jobs = new List<Job>();
public IEnumerable<Job> GetJobs()
{
return jobs;
}
public IEnumerable<Job> GetJobs(int maximumRows, int startRowIndex)
{
return jobs;
}
int totalJobsCount;
public int GetTotalJobsCount()
{
return totalJobsCount;
}
#endregion
protected void button_Click(object sender, EventArgs e)
{
}
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用System.Web.UI.WebControl;
公共部分类GridViewTest:System.Web.UI.Page,IJobDsPage
{
bool gridNeedsBinding=false;
受保护的无效页面加载(对象发送方、事件参数e)
{
如果(!IsPostBack)
{
gridNeedsBinding=true;
}
}
受保护的无效作业gV_页面索引交换(对象发送方,GridViewPageEventArgs e)
{
var gv=(GridView)发送方;
newPageIndexForGv=e.NewPageIndex;
gridNeedsBinding=true;
}
private int newPageIndexForGv=0;
受保护的无效页面\u预渲染完成(对象发送方,事件参数e)
{
如果(gridNeedsBinding)
{
//将数据提取到this.jobs和this.totalJobsCount中进行模拟
//该数据刚刚异步可用
JobDal=新的JobDal();
jobs=dal.GetJobs(jobsGv.PageSize,jobsGv.PageSize*newPageIndexForGv.ToList();
totalJobsCount=dal.GetTotalJobsCount();
//现在数据可用了,请绑定gridview
jobsGv.DataBind();
jobsGv.SetPageIndex(newPageIndexForGv);
}
}
#区域JobDsPage成员
列表作业=新列表();
公共IEnumerable GetJobs()
{
返回工作岗位;
}
公共IEnumerable GetJobs(int maximumRows,int startRowIndex)
{
返回工作岗位;
}
int TotalJobsont;
public int GetTotalJobsCount()
{
返回总工单;
}
#端区
受保护的无效按钮\u单击(对象发送者,事件参数e)
{
}
}
最后是一些类来将其连接在一起。我已在App_code中的一个代码文件中将这些代码组合在一起:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
/// <summary>
/// Simple POCO to use as row data in GridView
/// </summary>
public class Job
{
public int JobId { get; set; }
public string Description { get; set; }
public int MinLvl { get; set; }
//etc
}
/// <summary>
/// This will simulate a DAL that fetches data
/// </summary>
public class JobDal
{
private static int totalCount = 50; // let's pretend that db has total of 50 job records
public IEnumerable<Job> GetJobs()
{
return Enumerable.Range(0, totalCount).Select(i =>
new Job() { JobId = i, Description = "Descr " + i, MinLvl = i % 10 }); //simulate getting all records
}
public IEnumerable<Job> GetJobs(int maximumRows, int startRowIndex)
{
int count = (startRowIndex + maximumRows) > totalCount ? totalCount - startRowIndex : maximumRows;
return Enumerable.Range(startRowIndex, count).Select(i =>
new Job() { JobId = i, Description = "Descr " + i, MinLvl = i % 10 }); //simulate getting one page of records
}
public int GetTotalJobsCount()
{
return totalCount; // simulate counting total amount of rows
}
}
/// <summary>
/// Interface for our page, so we can call methods in the page itself
/// </summary>
public interface IJobDsPage
{
IEnumerable<Job> GetJobs();
IEnumerable<Job> GetJobs(int maximumRows, int startRowIndex);
int GetTotalJobsCount();
}
/// <summary>
/// This will be used by our ObjectDataSource
/// </summary>
public class JobObjectDs
{
public IEnumerable<Job> GetJobs()
{
var currentPageAsIJobDsPage = (IJobDsPage)HttpContext.Current.CurrentHandler;
return currentPageAsIJobDsPage.GetJobs();
}
public IEnumerable<Job> GetJobs(int maximumRows, int startRowIndex)
{
var currentPageAsIJobDsPage = (IJobDsPage)HttpContext.Current.CurrentHandler;
return currentPageAsIJobDsPage.GetJobs(maximumRows, startRowIndex);
}
public int GetTotalJobsCount()
{
var currentPageAsIJobDsPage = (IJobDsPage)HttpContext.Current.CurrentHandler;
return currentPageAsIJobDsPage.GetTotalJobsCount();
}
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用System.Web;
///
///在GridView中用作行数据的简单POCO
///
公开课工作
{
public int JobId{get;set;}
公共字符串说明{get;set;}
public int MinLvl{get;set;}
//等
}
///
///这将模拟获取数据的DAL
///
公营职业
{
private static int totalCount=50;//假设db总共有50条作业记录
公共IEnumerable GetJobs()
{
返回可枚举范围(0,totalCount)。选择(i=>
新建作业(){JobId=i,Description=“Descr”+i,MinLvl=i%10};//模拟获取所有记录
}
公共IEnumerable GetJobs(int maximumRows,int startRowIndex)
{
整数计数=(startRowIndex+maximumRows)>totalCount?totalCount-startRowIndex:maximumRows;
返回可枚举的.Range(startRowIndex,count)。选择(i=>
new Job(){JobId=i,Description=“Descr”+i,MinLvl=i%10});//模拟获取一页记录
}
public int GetTotalJobsCount()
{
return totalCount;//模拟计算行的总数
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.UI.WebControls;
public partial class GridViewTest : System.Web.UI.Page, IJobDsPage
{
bool gridNeedsBinding = false;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
gridNeedsBinding = true;
}
}
protected void jobsGv_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
var gv = (GridView)sender;
newPageIndexForGv = e.NewPageIndex;
gridNeedsBinding = true;
}
private int newPageIndexForGv = 0;
protected void Page_PreRendercomplete(object sender, EventArgs e)
{
if (gridNeedsBinding)
{
// fetch data into this.jobs and this.totalJobsCount to simulate
// that data has just become available asynchronously
JobDal dal = new JobDal();
jobs = dal.GetJobs(jobsGv.PageSize, jobsGv.PageSize * newPageIndexForGv).ToList();
totalJobsCount = dal.GetTotalJobsCount();
//now that data is available, bind gridview
jobsGv.DataBind();
jobsGv.SetPageIndex(newPageIndexForGv);
}
}
#region JobDsPage Members
List<Job> jobs = new List<Job>();
public IEnumerable<Job> GetJobs()
{
return jobs;
}
public IEnumerable<Job> GetJobs(int maximumRows, int startRowIndex)
{
return jobs;
}
int totalJobsCount;
public int GetTotalJobsCount()
{
return totalJobsCount;
}
#endregion
protected void button_Click(object sender, EventArgs e)
{
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
/// <summary>
/// Simple POCO to use as row data in GridView
/// </summary>
public class Job
{
public int JobId { get; set; }
public string Description { get; set; }
public int MinLvl { get; set; }
//etc
}
/// <summary>
/// This will simulate a DAL that fetches data
/// </summary>
public class JobDal
{
private static int totalCount = 50; // let's pretend that db has total of 50 job records
public IEnumerable<Job> GetJobs()
{
return Enumerable.Range(0, totalCount).Select(i =>
new Job() { JobId = i, Description = "Descr " + i, MinLvl = i % 10 }); //simulate getting all records
}
public IEnumerable<Job> GetJobs(int maximumRows, int startRowIndex)
{
int count = (startRowIndex + maximumRows) > totalCount ? totalCount - startRowIndex : maximumRows;
return Enumerable.Range(startRowIndex, count).Select(i =>
new Job() { JobId = i, Description = "Descr " + i, MinLvl = i % 10 }); //simulate getting one page of records
}
public int GetTotalJobsCount()
{
return totalCount; // simulate counting total amount of rows
}
}
/// <summary>
/// Interface for our page, so we can call methods in the page itself
/// </summary>
public interface IJobDsPage
{
IEnumerable<Job> GetJobs();
IEnumerable<Job> GetJobs(int maximumRows, int startRowIndex);
int GetTotalJobsCount();
}
/// <summary>
/// This will be used by our ObjectDataSource
/// </summary>
public class JobObjectDs
{
public IEnumerable<Job> GetJobs()
{
var currentPageAsIJobDsPage = (IJobDsPage)HttpContext.Current.CurrentHandler;
return currentPageAsIJobDsPage.GetJobs();
}
public IEnumerable<Job> GetJobs(int maximumRows, int startRowIndex)
{
var currentPageAsIJobDsPage = (IJobDsPage)HttpContext.Current.CurrentHandler;
return currentPageAsIJobDsPage.GetJobs(maximumRows, startRowIndex);
}
public int GetTotalJobsCount()
{
var currentPageAsIJobDsPage = (IJobDsPage)HttpContext.Current.CurrentHandler;
return currentPageAsIJobDsPage.GetTotalJobsCount();
}
}