在实际开发中,常常会遇到很多信息,由于字段比较多,常常用GridView或RadGrid等一些数据显示控件直接一行显示,效果极差(需要拖动滚动条,比较麻烦)。
所以自然想到定制模板用table来实现:
还是先上代码,然后再分析吧。
<asp:FormView ID="fvBaseInfo" runat="server" Width="100%" AllowPaging="true"
onpageindexchanging="fvBaseInfo_PageIndexChanging">
<ItemTemplate>
<table width="100%" class="Mtable">
<tr>
<td colspan="8" align="center"><font color="red" size="4">未缴费单位基本信息表</font></td>
</tr>
<tr>
<td align="right">审批机关:</td>
<td align="center">
<asp:Label ID="Label1" runat="server" Text='<%# Eval("SPJG1") %>' ForeColor="ButtonShadow"></asp:Label>
</td>
<td align="right">监督管理机关:</td>
<td align="center">
<asp:Label ID="Label2" runat="server" Text='<%# Eval("JDGLJG1") %>' ForeColor="ButtonShadow"></asp:Label>
</td>
<td align="right">取水权人名称:</td>
<td align="center">
<asp:Label ID="Label3" runat="server" Text='<%# Eval("QSQRMC") %>' ForeColor="ButtonShadow"></asp:Label>
</td>
<td align="right">法人代表:</td>
<td align="center">
<asp:Label ID="Label4" runat="server" Text='<%# Eval("FDDBR") %>' ForeColor="ButtonShadow"></asp:Label>
</td>
</tr>
<tr>
<td align="right">职务:</td>
<td align="center">
<asp:Label ID="Label5" runat="server" Text='<%# Eval("ZW") %>' ForeColor="ButtonShadow"></asp:Label>
</td>
<td align="right">单位类型:</td>
<td align="center">
<asp:Label ID="Label6" runat="server" Text='<%# Eval("DWLX") %>' ForeColor="ButtonShadow"></asp:Label>
</td>
<td align="right">行业类别:</td>
<td align="center">
<asp:Label ID="Label7" runat="server" Text='<%# Eval("HYLB") %>' ForeColor="ButtonShadow"></asp:Label>
</td>
<td align="right">取水申请日期:</td>
<td align="center">
<asp:Label ID="Label8" runat="server" Text='<%# this.GetSubString(Eval("SQQSQSRQ").ToString()) %>' ForeColor="ButtonShadow"></asp:Label>
</td>
</tr>
<tr>
<td align="right">许可证有效期:</td>
<td align="center">
<asp:Label ID="Label9" runat="server" Text='<%# this.GetSubString(Eval("XKZYXQ").ToString()) %>' ForeColor="ButtonShadow"></asp:Label>
</td>
<td align="right">通讯地址:</td>
<td align="center">
<asp:Label ID="Label10" runat="server" Text='<%# Eval("TXDZ") %>' ForeColor="ButtonShadow"></asp:Label>
</td>
<td align="right">邮政编码:</td>
<td align="center">
<asp:Label ID="Label11" runat="server" Text='<%# Eval("YZBM") %>' ForeColor="ButtonShadow"></asp:Label>
</td>
<td align="right">联系人:</td>
<td align="center">
<asp:Label ID="Label12" runat="server" Text='<%# Eval("LXR") %>' ForeColor="ButtonShadow"></asp:Label>
</td>
</tr>
<tr>
<td align="right">工作部门:</td>
<td align="center">
<asp:Label ID="Label13" runat="server" Text='<%# Eval("GZBM") %>' ForeColor="ButtonShadow"></asp:Label>
</td>
<td align="right">职务(职称):</td>
<td align="center">
<asp:Label ID="Label14" runat="server" Text='<%# Eval("ZWZC") %>' ForeColor="ButtonShadow"></asp:Label>
</td>
<td align="right">联系电话:</td>
<td align="center">
<asp:Label ID="Label15" runat="server" Text='<%# Eval("LXDH1") %>' ForeColor="ButtonShadow"></asp:Label>
</td>
<td align="right">电子邮箱:</td>
<td align="center">
<asp:Label ID="Label16" runat="server" Text='<%# Eval("DZXX1") %>' ForeColor="ButtonShadow"></asp:Label>
</td>
</tr>
</table>
</ItemTemplate>
<EmptyDataTemplate>
<table width="100%">
<tr>
<td align="center"><font color="red" size="3">无任何信息</font></td>
</tr>
</table>
</EmptyDataTemplate>
</asp:FormView>
以上是在FormView里的模板列里放置了一个table,用来显示较为多的字段信息,提供浏览,这时就涉及到如果获取到的信息数量特别多的话,分页该如何实现?
...
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
VSDataTable1 = new DataTable(); //未缴费单位基本信息
}
}
.
.
.
protected void btnQuery_Click(object sender, EventArgs e)
{
string strSQL ="select * from QSXK_DJB1";
DM_Water dm = new DM_Water();
DataSet ds = dm.getsql(strSQL);
this.fvBaseInfo.DataSource = ds;
this.fvBaseInfo.DataBind();
ViewState["wjfdwjbxx"] = ds.Tables[0];
}
...
//FormView控件分页
protected void fvBaseInfo_PageIndexChanging(object sender, FormViewPageEventArgs e)
{
//分页代码
this.fvBaseInfo.DataSource = VSDataTable1;
this.fvBaseInfo.PageIndex = e.NewPageIndex;
this.fvBaseInfo.DataBind();
}
...
/// <summary>
/// 存放在ViewState的DataTable 未缴费单位基本信息表
/// </summary>
private DataTable VSDataTable1
{
get { return ViewState["wjfdwjbxx"] as DataTable; }
set { ViewState["wjfdwjbxx"] = value; }
}
...
/// <summary>
/// 普通的绑定资料
/// </summary>
private void PageDataBind()
{
fvBaseInfo.DataSource = VSDataTable1;
fvBaseInfo.DataBind();
}
...
/// <summary>
/// 绑定资料加换页
/// </summary>
/// <param name="getPageIndex">新页面Index</param>
private void PageDataBind(Int32 getPageIndex)
{
this.fvBaseInfo.DataSource = VSDataTable1;
fvBaseInfo.PageIndex = getPageIndex;
fvBaseInfo.DataBind();
}
这里比较重要的就是利用ViewState来保存数据源信息,以供分页来使用,效果图如下地址所示:
http://hi.csdn.net/space-95339-do-album-picid-551779.html
这里只是个人对于DataTable分页的一种实现方案,如有不妥之处,敬请指正,大家一起交流学习。