asp.net 动态表单之数据分页(2)


protected void btnSearch_Click(object sender, EventArgs e)
{
SearchNow = true;
VirtualPager1.CurrentPageIndex = 1;
BindTable();
}
protected void VirtualPager1_PageIndexChanged(object sender, Botwave.Web.Controls.PageChangedEventArgs e)
{
BindTable();
}
/// <summary>
/// 获取考核成绩.
/// </summary>
/// <returns></returns>
private DataTable GetData(int cellsCount)
{
int nRecordCount = 0;
string condition = "";
string filterName = txtUserName.Text.Trim();//姓名搜索.
StringBuilder sb = new StringBuilder();
sb.AppendFormat(" GroupID ='{0}'", GroupID);
if (!string.IsNullOrEmpty(filterName))
sb.AppendFormat(" AND (UserName like '%{0}%' OR RealName like '%{0}%')", filterName);
condition = sb.ToString();
if (SearchNow)//当在很多页的时候,或者是最后一页,这个时候来点击查询,就会报错了CurrentPageIndex,这个做法就是为了防止这个.
VirtualPager1.CurrentPageIndex = 0;
DataTable dt = personalGradeService.GetGradeData(VirtualPager1.CurrentPageIndex, VirtualPager1.ItemsPerPage * cellsCount, condition, ref nRecordCount);
VirtualPager1.TotalRecordCount = nRecordCount / cellsCount;
VirtualPager1.DataBind();
SearchNow = false;//要重新复制.
return dt;
}


业务逻辑(分页)

复制代码 代码如下:


public System.Data.DataTable GetGradeData(int currentPageIndex, int pageSize, string condition, ref int nRecordCount)
{
string fieldShow = " UserID,TemplateCode,TemplateItemCode,GradeData,ID,UserName,RealName ";
string fieldOrder = " UserName ASC,TemplateItemCode ASC ";
string where = condition;
if (String.IsNullOrEmpty(where))
{
where = "1=1";
}
return IBatisDbHelper.GetPagedList("dbo.vw_UserGradeData", "ID", currentPageIndex, pageSize, fieldShow, fieldOrder, where, ref nRecordCount);
}


注意事项:
在使用<asp:Literal runat="server"></asp:Literal>来在页面输出html来显示表单的话,要注意字符串的长度,如果html太长的话,就会出错了,因为Literal的有8000个字符的限制的;
在数据库中对数据进行好排序,这样才能保证数据跟表头是一一对应的;
这里有个前提,那就是查询出来的数据都是包含同等多的记录的,比如同学A和同学B的科目是一样多的;因为不一样多,那这个动态表单就没什么意义了。如果真的有不同,也是可以做出来的,但是会麻烦一点;

您可能感兴趣的文章:

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wjjxgx.html