当前位置: 移动技术网 > IT编程>开发语言>.net > C#列表页面后台代码

C#列表页面后台代码

2019年01月08日  | 移动技术网IT编程  | 我要评论

qq飞车调皮泼猴,国政通科技有限公司,红塔山会员俱乐部

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;
            }
        }
    }
}

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网