C#列表页面后台代码

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using Oracle.ManagedDataAccess.Client;

public partial class Page_Index : System.Web.UI.Page
{
    DBService db = new DBService();
    OracleConn conn = new OracleConn();
    private int id = 0; //保存指定行操作所在的ID号
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            //设置当前页的索引
            int pageIndex = 1;
            try
            {
                //获取当前索要跳转页的索引号
                pageIndex = Convert.ToInt32(Request.QueryString["Page"]);
                //如果是第0页将会跳转入第1页
                if (pageIndex <= 0)
                {
                    pageIndex = 1;
                }
            }
            catch
            {
                pageIndex = 1;
            }

            DataTable dt = this.GetDataTable(); //获取绑定的数据表

            PagedDataSource pds = new PagedDataSource();    //创建分页数据源对象                
            pds.DataSource = dt.DefaultView;    //为数据源对象设置数据源
            pds.AllowPaging = true; //对象允许分页
            pds.PageSize = 2;   //设置对象每页显示的大小
            pds.CurrentPageIndex = pageIndex - 1; //设置数据源的当前页

            //向Repeater控件上绑定分页数据源控件
            this.Repeater1.DataSource = pds;
            this.Repeater1.DataBind();

            //使用Literal标签来动态指定每个标签跳转页的链接
            ltlPageBar.Text = this.GetPageBar(pds);
        }
    }
    /// <summary>
    /// 将数据源绑定Repeater控件上
    /// </summary>
    private void DataBindToRepeater()
    {
        OracleCommand cmd = conn.GetData();
        this.Repeater1.DataSource = cmd.ExecuteReader();    //为Repeater对象指定数据源
        this.Repeater1.DataBind(); //绑定数据源
    }
    /// <summary>
    /// 获取每个标签上的跳转页的链接地址
    /// </summary>
    /// <param name="pds">分页数据源对象</param>
    /// <returns>分页操作按钮的html文本</returns>
    private string GetPageBar(PagedDataSource pds)
    {
        string pageBar = string.Empty;  //声明页面标签文本
        int currentPageIndex = pds.CurrentPageIndex + 1;    //获取当前页索引

        //判断首页的链接页面
        if (currentPageIndex == 1)  //如果该页为第一页,则证明它为首页
        {
            pageBar += "<a href="javascript:void(0)">首页</a>";
        }
        else
        {
            //如果不是首页,首页链接的地址将会为1
            pageBar += "<a href="" + Request.CurrentExecutionFilePath + "?Page=1">首页</a>";
        }

        //判断上一页链接的地址
        if ((currentPageIndex - 1) < 1) //如果上一页小于1则链接到第一页
        {
            pageBar += "<a href="javascript:void(0)">上一页</a>";
        }
        else
        {
            //如果上一页地址不是1将会链接到上一页
            pageBar += "<a href="" + Request.CurrentExecutionFilePath + "?Page=" + (currentPageIndex - 1) + "">上一页</a>";
        }

        //指定下一页的链接地址
        if ((currentPageIndex + 1) > pds.PageCount)
        {
            //如果下一页的地址大于总页数,将会连接到首页
            pageBar += "<a href="javascript:void(0)">下一页</a>";
        }
        else
        {
            //否则的话链接到下一页
            pageBar += "<a href="" + Request.CurrentExecutionFilePath + "?Page=" + (currentPageIndex + 1) + "">下一页</a>";
        }

        //指定末页的链接地址
        if (currentPageIndex == pds.PageCount)
        {
            pageBar += "<a href="javascript:void(0)">末页</a>";
        }
        else
        {
            pageBar += "<a href="" + Request.CurrentExecutionFilePath + "?Page=" + pds.PageCount + "">末页</a>";
        }

        return pageBar; //返回html文本
    }

    /// <summary>
    /// 获取数据源,重新链接数据
    /// </summary>
    /// <returns>DataTable,数据源</returns>
    private DataTable GetDataTable()
    {
        DataTable dt = new DataTable(); //创建数据库表
        dt = conn.GetTableData();
        return dt;
    }
    private void GetDataBind()
    {
        Repeater1.DataSource = conn.GetTableData();
        Repeater1.DataBind();
    }
    protected void btnConnect_Click(object sender, EventArgs e)
    {
        OracleConn conn = new OracleConn();
        int result = conn.GetConn();
        if (result == 1)
        {
            Response.Write("连接成功!");
        }
    }
    protected void btnAdd_Click(object sender, EventArgs e)
    {
        Response.Redirect("Add.aspx");
    }
    protected void btnGetData_Click(object sender, EventArgs e)
    {
        //查询数据点击事件
        GetDataBind();
    }
    protected void Repeater1_ItemCommand(object source, RepeaterCommandEventArgs e)
    {
        //获取命令文本,判断发出的命令为何种类型,根据命令类型调用事件
        if (e.CommandName == "Edit")  //编辑命令
        {
            id = int.Parse(e.CommandArgument.ToString());   //获取命令ID号
        }
        else if (e.CommandName == "Cancel")   //取消更新命令
        {
            id = -1;
        }
        else if (e.CommandName == "Delete")    //删除行内容命令
        {
            id = int.Parse(e.CommandArgument.ToString());   //获取删除行的ID号
            //删除选定的行,并重新指定绑定操作
            this.DeleteRepeater(id);
        }
        else if (e.CommandName == "Update") //更新行内容命令
        {
            //获取更新行的内容和ID号
            string strText = ((TextBox)e.Item.FindControl("txtName")).Text.Trim();
            int intId = int.Parse(((Label)e.Item.FindControl("lblID")).Text);
            //更新Repeater控件的内容
            this.UpdateRepeater(intId,strText);
        }

        //重新绑定控件上的内容
        this.DataBindToRepeater();
    }
    /// <summary>
    /// 删除行内容
    /// </summary>
    /// <param name="intId">删除行所在内容的ID</param>
    private void DeleteRepeater(int intId)
    {
        conn.DeleteData(intId);
    }
    /// <summary>
    /// 更新Repeater控件中的内容
    /// </summary>
    /// <param name="strText">修改后的内容</param>
    /// <param name="intId">内容所在行的ID号</param>
    private void UpdateRepeater( int intId,string strText)
    {
        conn.UpdateData(intId,strText);
    }
    protected void Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {

        //判断Repeater控件中的数据是否是绑定的数据源,如果是的话将会验证是否进行了编辑操作
        //ListItemType 枚举表示在一个列表控件可以包括,例如 DataGrid、 DataList和 Repeater 控件的不同项目。 
        if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
        {
            //获取绑定的数据源,这里要注意上面使用sqlReader的方法来绑定数据源,所以下面使用的DbDataRecord方法获取的
            //如果绑定数据源是DataTable类型的使用下面的语句就会报错.
            //System.Data.Common.DbDataRecord record = (System.Data.Common.DbDataRecord)e.Item.DataItem;
            
            //DataTable类型的数据源验证方式
            System.Data.DataRowView record = (DataRowView)e.Item.DataItem;

            //判断数据源的id是否等于现在的id,如果相等的话证明现点击了编辑触发了userRepeat_ItemCommand事件
            if (id == int.Parse(record["id"].ToString()))
            {
                ((Panel)e.Item.FindControl("plItem")).Visible = false;
                ((Panel)e.Item.FindControl("plEdit")).Visible = true;
            }
            else
            {
                ((Panel)e.Item.FindControl("plItem")).Visible = true;
                ((Panel)e.Item.FindControl("plEdit")).Visible = false;
            }
        }
    }
}

原文地址:https://www.cnblogs.com/missheyo/p/10234415.html