zoukankan      html  css  js  c++  java
  • Repeater 数据绑定 分页

      最近有使用到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();
    
        }
        
    }
  • 相关阅读:
    使用CustomValidate自定义验证控件
    C#中金额的大小写转换
    Andriod出错之Unable to build: the file dx.jar was not loaded from the SDK folder!
    VC 编写的打字练习
    机房工作笔记Ping只有单向通
    web服务协同学习笔记(1)
    Dll 学习3 将MDI子窗口封装在DLL中
    机房工作学习文件共享
    Andriod出错之Failed to find an AVD compatible with target 'Android 2.2'
    Andriod出错之wrapper was not properly loaded first
  • 原文地址:https://www.cnblogs.com/dreamskies/p/3448165.html
Copyright © 2011-2022 走看看