最近有使用到repeater来做数据绑定查询,是用来记录后台校验数据错误记录的临时查询。因为原先使用的都是从数据库中直接读表方式绑定,同时项目中有写好的分页控件,这次因为使用自定义错误记录并显示,不再将错误写到数据库中的表里,然后再查询的样式,所以就搜索整理了下。
(定义较为简单,有待完善)
前台代码:
<body> <form id="form1" runat="server"> <div> <table class="repTable "> <asp:Repeater ID="dataTbe" runat="server"> <HeaderTemplate> <tr class="repHead"> <td scope="col" style=" 10%"> 源数据来源 </td> <td scope="col" style=" 8%"> 标识号 </td> <td scope="col" style=" 8%"> 出错字段 </td> <td scope="col" style=" 30%"> 出错原因 </td> </tr> </HeaderTemplate> <ItemTemplate> <tr class="repItem"> <td scope="col" style=" 10%"> <%#Eval("DataSource")%> </td> <td scope="col" style=" 8%"> <%#Eval("IBSCLNO")%> </td> <td scope="col" style=" 8%"> <%#Eval("ErrField")%> </td> <td scope="col" style=" 30%"> <%#Eval("ErrDesc")%> </td> </tr> </ItemTemplate> </asp:Repeater> </table> <asp:LinkButton ID="lbtnFirstPage" runat="server" OnClick="lbtnFirstPage_Click">页首</asp:LinkButton> <asp:LinkButton ID="lbtnpritPage" runat="server" OnClick="lbtnpritPage_Click">上一页</asp:LinkButton> <asp:LinkButton ID="lbtnNextPage" runat="server" OnClick="lbtnNextPage_Click">下一页</asp:LinkButton> <asp:LinkButton ID="lbtnDownPage" runat="server" OnClick="lbtnDownPage_Click">页尾</asp:LinkButton><br /> 第<asp:Label ID="labPage" runat="server" Text="1"></asp:Label>页/共<asp:Label ID="LabCountPage" runat="server" Text="Label"></asp:Label>页 </div> </form> </body>
后台代码:
using System; using System.Collections; using System.Configuration; using System.Data; using System.Linq; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.HtmlControls; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Xml.Linq; public partial class SDNLMess_tESTY : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { this.labPage.Text = "1"; DtBind(); } } protected void DtBind(){ DataTable datadt = new DataTable(); datadt.Columns.Add(new DataColumn("DataSource")); //数据来源 datadt.Columns.Add(new DataColumn("IBSCLNO")); //主键 datadt.Columns.Add(new DataColumn("ErrField")); //出错的字段 datadt.Columns.Add(new DataColumn("ErrDesc")); //出错的原因 datadt.Rows.Add(new object[4] { "临时", "123456", "yao", "断电" });//自定义写入dt中的数据(测试使用) datadt.Rows.Add(new object[4] { "临时", "123456", "yao", "断电" }); datadt.Rows.Add(new object[4] { "临时", "123456", "yao", "断电" }); datadt.Rows.Add(new object[4] { "临时", "123456", "yao", "断电" }); datadt.Rows.Add(new object[4] { "临时", "123456", "yao", "断电" }); datadt.Rows.Add(new object[4] { "临时", "123456", "yao", "断电" }); datadt.Rows.Add(new object[4] { "临时", "123456", "yao", "断电" }); datadt.Rows.Add(new object[4] { "临时", "123456", "yao", "断电" }); datadt.Rows.Add(new object[4] { "临时", "123456", "yao", "断电" }); //dataTbe.DataSource = datadt; //dataTbe.DataBind(); PagedDataSource pds = new PagedDataSource(); pds.DataSource = datadt.DefaultView; pds.AllowPaging = true;//启用分页 pds.PageSize = 2;//设置每页显示条数 pds.CurrentPageIndex = Convert.ToInt32(this.labPage.Text) - 1;//获取当前也索引 dataTbe.DataSource = pds;//绑定数据源 LabCountPage.Text = pds.PageCount.ToString();//得到页面总数 labPage.Text = (pds.CurrentPageIndex + 1).ToString();//得到当前显示页面数 this.lbtnpritPage.Enabled = true;//控制控件的显示与隐藏 this.lbtnFirstPage.Enabled = true; this.lbtnNextPage.Enabled = true; this.lbtnDownPage.Enabled = true; if (pds.CurrentPageIndex + 1 == pds.PageCount)//当当前页为最后一页时控制下一页和尾页控件不可用 { this.lbtnNextPage.Enabled = false; this.lbtnDownPage.Enabled = false; } if (labPage.Text == "1")//当当前页为第一页时控制第一页控件不可用 { this.lbtnpritPage.Enabled = false; } dataTbe.DataBind();//repeater数据绑定 } protected void lbtnpritPage_Click(object sender, EventArgs e)//上一页按钮事件 { this.labPage.Text = Convert.ToString(Convert.ToInt32(labPage.Text) - 1); this.DtBind(); } protected void lbtnFirstPage_Click(object sender, EventArgs e)//首页按钮事件 { this.labPage.Text = "1"; this.DtBind(); } protected void lbtnDownPage_Click(object sender, EventArgs e)//下一页按钮事件 { this.labPage.Text = this.LabCountPage.Text; this.DtBind(); } protected void lbtnNextPage_Click(object sender, EventArgs e)//尾页事件 { this.labPage.Text = Convert.ToString(Convert.ToInt32(labPage.Text) + 1); this.DtBind(); } }