动态表单之数据分页
2010-03-16 19:10 听风吹雨 阅读(3102) 评论(0) 编辑 收藏 举报 我们很常会在项目中提到一个动态表单的概念,比如学校里面学生的考试成绩,当学生登录系统的时候,他当然是希望看到他自己所有科目
的成绩;又或者是班主任,他需要看到本班同学所有科目的成绩;这些时候我们一般都会在页面中呈现如下的效果。
但是问题来了,不同科系的同学的科目是不一样的,那么我们在数据库设计的时候通常是把学生、某科成绩作为一条记录,那么这个时候我们就需要做一个行转列的
逻辑处理了。
- 解决方法:
- 使用GridView来生成表单,这个实现起来会比较麻烦,如果要在列表里面显示链接就更不可能了;
-
生成html再输出到页面中,这个实现起来比较灵活、方便;
- 基本功能点:
- 动态生成表 头;
- 数据进行分页;
- 查询数 据;
- 对每个成绩进行超链接,查看明细;
-
页面代码
<div id="dataDiv1">
<asp:Literal ID="labtable" runat="server"></asp:Literal>
<div class="toolBlock" style="border-top:solid 1px #C0CEDF; padding-top:5px">
<bw:VirtualPager ID="VirtualPager1" runat="server" ItemsPerPage="10" OnPageIndexChanged="VirtualPager1_PageIndexChanged" />
</div>
</div>
<asp:Literal ID="labtable" runat="server"></asp:Literal>
<div class="toolBlock" style="border-top:solid 1px #C0CEDF; padding-top:5px">
<bw:VirtualPager ID="VirtualPager1" runat="server" ItemsPerPage="10" OnPageIndexChanged="VirtualPager1_PageIndexChanged" />
</div>
</div>
- 后台代码(生 成html字符串)
/// <summary>
/// table方式加载考核成绩.
/// Step1:通过年月来获取唯一的批次.
/// Step2:生成表头.
/// Step3:开始循环每个用户.
/// Step4:循环每个用户的考核项.
/// </summary>
protected void BindTable()
{
labtable.Text = string.Empty;
//通过年月来获取唯一的批次.
string year = DropDownImportYear.SelectedValue;
string month = DropDownImportMonth.SelectedValue;
string group_KH_Date = year + month;
GroupService groupService = new GroupService();
DataTable dtGroup = groupService.GetByTemplateCodeAndGroup_KH_Date(TemplateCode, group_KH_Date);
if (dtGroup.Rows.Count > 0)
{
Guid groupID = (Guid)DbUtils.ToGuid(dtGroup.Rows[0]["ID"]);
int groupStutas = DbUtils.ToInt32(dtGroup.Rows[0]["GroupStutas"].ToString());
DataTable dtCells = personalGradeService.GetDistinctTemplateItemNameByGroupID(groupID);
GroupID = groupID.ToString();
if (dtCells.Rows.Count > 0)
{
int cellsCount = 0;//考核项数.
StringBuilder strtable = new StringBuilder();
strtable.AppendFormat("<table cellpadding=\"0\" cellspacing=\"0\" class=\"tblClass\">");
strtable.AppendFormat("<tr>");
strtable.AppendFormat("<th>用户名</th>< /span>");
cellsCount = dtCells.Rows.Count;
for (int i = 0; i < cellsCount; i++)
{
strtable.AppendFormat("<th>{0}</th>", dtCells.Rows[i]["Name"].ToString());
}
strtable.AppendFormat("</tr>");
DataTable dtGradeData = GetData(cellsCount);
int cursor = 1;//表示一个用户的第一条记录.
string userName = string.Empty;
string realName = string.Empty;
for (int i = 0; i<dtGradeData.Rows.Count; i++)
{
if (cursor == 1)
{
userName = dtGradeData.Rows[i]["UserName"].ToString();
realName = dtGradeData.Rows[i]["RealName"].ToString();
strtable.AppendFormat("<td width=\"12%\">{0}</td>", realName);
}
string id = dtGradeData.Rows[i]["ID"].ToString();
string templateItemCode = dtGradeData.Rows[i]["TemplateItemCode"].ToString();
strtable.AppendFormat("<td width=\"50px\">");
string GradeData = dtGradeData.Rows[i]["GradeData"].ToString();
if (!string.IsNullOrEmpty(GradeData) && GradeData != "0")
{
GradeData=Math.Round(decimal.Parse(GradeData), 2).ToString();
//转换成两位小数
}
strtable.AppendFormat("{0}", GradeData);
//未发布的成绩才能修改
if (groupStutas != (int)Botwave.MRPS.Constants.GroupStutas.FinishPush)
{
strtable.AppendFormat("<br /><a style=\"text-decoration:underline\" class=\"thickbox\" href='UserGradeDataEdit.aspx?ID={0}&BeModifer={1}&KeepThis=true&TB_iframe=true&height=250&width=500'>", id, userName);
strtable.AppendFormat("<img src=\"../res/gradeedit.gif\" align=\"baseline\" title=\"修改成绩\" />");
strtable.AppendFormat("</a>");
}
strtable.AppendFormat(" <a target=\"_blank\" style=\"text-decoration:underline\" href='http://www.cnblogs.com/CheckObjectManager/pages/LogScoreChangeDetail.aspx?GroupId={0}&TemplateCode={1}&TemplateItemCode={2}&UserName={3}&returnUrl='>", groupID, TemplateCode, templateItemCode, userName);
strtable.AppendFormat("<img src=\"../res/gradelog.gif\" align=\"baseline\" title=\"查看日志\" />");
strtable.AppendFormat("</a>");
strtable.AppendFormat("</td>");
if (cursor < cellsCount)
cursor++;
else
{
strtable.AppendFormat("</tr>");
cursor = 1;
}
}
strtable.AppendFormat("</table>");
labtable.Text = strtable.ToString();
}
}
}
/// table方式加载考核成绩.
/// Step1:通过年月来获取唯一的批次.
/// Step2:生成表头.
/// Step3:开始循环每个用户.
/// Step4:循环每个用户的考核项.
/// </summary>
protected void BindTable()
{
labtable.Text = string.Empty;
//通过年月来获取唯一的批次.
string year = DropDownImportYear.SelectedValue;
string month = DropDownImportMonth.SelectedValue;
string group_KH_Date = year + month;
GroupService groupService = new GroupService();
DataTable dtGroup = groupService.GetByTemplateCodeAndGroup_KH_Date(TemplateCode, group_KH_Date);
if (dtGroup.Rows.Count > 0)
{
Guid groupID = (Guid)DbUtils.ToGuid(dtGroup.Rows[0]["ID"]);
int groupStutas = DbUtils.ToInt32(dtGroup.Rows[0]["GroupStutas"].ToString());
DataTable dtCells = personalGradeService.GetDistinctTemplateItemNameByGroupID(groupID);
GroupID = groupID.ToString();
if (dtCells.Rows.Count > 0)
{
int cellsCount = 0;//考核项数.
StringBuilder strtable = new StringBuilder();
strtable.AppendFormat("<table cellpadding=\"0\" cellspacing=\"0\" class=\"tblClass\">");
strtable.AppendFormat("<tr>");
strtable.AppendFormat("<th>用户名</th>< /span>");
cellsCount = dtCells.Rows.Count;
for (int i = 0; i < cellsCount; i++)
{
strtable.AppendFormat("<th>{0}</th>", dtCells.Rows[i]["Name"].ToString());
}
strtable.AppendFormat("</tr>");
DataTable dtGradeData = GetData(cellsCount);
int cursor = 1;//表示一个用户的第一条记录.
string userName = string.Empty;
string realName = string.Empty;
for (int i = 0; i<dtGradeData.Rows.Count; i++)
{
if (cursor == 1)
{
userName = dtGradeData.Rows[i]["UserName"].ToString();
realName = dtGradeData.Rows[i]["RealName"].ToString();
strtable.AppendFormat("<td width=\"12%\">{0}</td>", realName);
}
string id = dtGradeData.Rows[i]["ID"].ToString();
string templateItemCode = dtGradeData.Rows[i]["TemplateItemCode"].ToString();
strtable.AppendFormat("<td width=\"50px\">");
string GradeData = dtGradeData.Rows[i]["GradeData"].ToString();
if (!string.IsNullOrEmpty(GradeData) && GradeData != "0")
{
GradeData=Math.Round(decimal.Parse(GradeData), 2).ToString();
//转换成两位小数
}
strtable.AppendFormat("{0}", GradeData);
//未发布的成绩才能修改
if (groupStutas != (int)Botwave.MRPS.Constants.GroupStutas.FinishPush)
{
strtable.AppendFormat("<br /><a style=\"text-decoration:underline\" class=\"thickbox\" href='UserGradeDataEdit.aspx?ID={0}&BeModifer={1}&KeepThis=true&TB_iframe=true&height=250&width=500'>", id, userName);
strtable.AppendFormat("<img src=\"../res/gradeedit.gif\" align=\"baseline\" title=\"修改成绩\" />");
strtable.AppendFormat("</a>");
}
strtable.AppendFormat(" <a target=\"_blank\" style=\"text-decoration:underline\" href='http://www.cnblogs.com/CheckObjectManager/pages/LogScoreChangeDetail.aspx?GroupId={0}&TemplateCode={1}&TemplateItemCode={2}&UserName={3}&returnUrl='>", groupID, TemplateCode, templateItemCode, userName);
strtable.AppendFormat("<img src=\"../res/gradelog.gif\" align=\"baseline\" title=\"查看日志\" />");
strtable.AppendFormat("</a>");
strtable.AppendFormat("</td>");
if (cursor < cellsCount)
cursor++;
else
{
strtable.AppendFormat("</tr>");
cursor = 1;
}
}
strtable.AppendFormat("</table>");
labtable.Text = strtable.ToString();
}
}
}
- 几个辅助方法(如何处理好分页问题,这就关系到cellsCount参数的使用 了,cellsCount表示科目数)
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;
}
{
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);
}
{
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 ID="labtable" runat="server"></asp:Literal>来在页面输出html来显示表单的话,要注意字
符串的长度,如果html太长的话,就会出错了,因为Literal的有8000个字符的限制的;
- 在数据库中对数据进行好排序,这样才能保证数据跟表头是一一对应的;
- 这里有个前提,那就是 查询出来的数据都是包含同等多的记录的,比如同学A和同学B的科目是一样多的;因为不一样多,那这个动态表单就没什么意义了。如果真的有不同,也是可以做 出来的,但是会麻烦一点;
作者:听风吹雨
出处:
http://www.cnblogs.com/gaizai/
邮箱:gaizai@126.com
版权:本文版权归作者和博客园共有
转载:欢迎转载,必须保留原文链接
格言:不喜欢是因为不会 && 因为会所以喜欢