[转] asp.net中repeater按钮传值与分页

我想要在repeater中每绑定一行数据时,都对应一个按钮,其实也就是做后台管理,但我在按键的执行事件中却总是得不到要传递的值,开始我是这样传的:

CommandArgument="<%#Eval('adminID') %>"//这一行是传值

CommandName="upd"//因为有两个按钮,所以会有这一行

当然,这都是按钮的属性,但这样执行时总是报错,实在不知其中意思了,把上面的代码改为如下:

CommandArgument='<%#Eval("adminID") %>'

CommandName="upd"

一切正常,郁闷~~~~~~~~~

当然,这一小篇的正式内容当然是repeater分页了,其实reapeater分页在前台的代码并不多,只是增加了几个按钮:首页、上一页、下一页、未页,对了还有(当前页/总页);对了,这里用的方法是asp.net集成的方法(前几天去面试,有一美女程序员问我编码实现数据分页,整理思路,感觉不难,如果手头的活忙完了,也可以一试)。

首先:后台对repeater绑定数据源上就有很大的差别:

主义的数据源不再是datasource而是:

        public static PagedDataSource pds = new PagedDataSource();//把它设置为类的属性,方便在类内调用

    public void bind(string str,int page)//做成方法为了方便点击   “首页”等按钮时调用
    {

        pds.DataSource = BLL.adminLogin.getdt(str).DefaultView;//为pds绑定数据源
        pds.AllowPaging = true;   //启用分页
        pds.PageSize = 18;    //每页显示记录条数
        pds.CurrentPageIndex = page;//设置第page页为起始页


        Repeater1.DataSource = pds; //最后再把数据源绑定给repeater
        Repeater1.DataBind();

        if (page == 0)   //决定“首页”等各个按钮的可用性,假如现在在第一页,那么“首页”和“上一页”一定不可以使用,不是吗??
        {
            display();
            Button1.Enabled = false;
            Button2.Enabled = false;
        }
        else if (page == (pds.PageCount - 1))
        {
            display();
            Button3.Enabled = false;
            Button4.Enabled = false;
        }
        else
        { display(); }

   }

   那么 ,在按钮的事件中,我就会很省力气了

    protected void Button1_Click(object sender, EventArgs e)
    {
        //这是首页
        bind(sql, 0);
        display();
        Button1.Enabled = false;
        Button2.Enabled = false;
    }
    protected void Button2_Click(object sender, EventArgs e)
    {
        //这是上一页
        bind(sql, pds.CurrentPageIndex - 1);
    }
    protected void Button3_Click(object sender, EventArgs e)
    {
        //这是下一页
        bind(sql, pds.CurrentPageIndex + 1);
    }
    protected void Button4_Click(object sender, EventArgs e)
    {
        //这是未页
        bind(sql, pds.PageCount - 1);
    }
    public void display() //这个方法使所以的按钮可用
    {
        Button1.Enabled = true;
        Button2.Enabled = true;
        Button3.Enabled = true;
        Button4.Enabled = true;
    }

然后,没有然后了,一切搞定,需要补充的就是,那几个按钮的位置并没有要求,只要觉得看上去漂亮,那么,什么地方都可以放的,呵呵 ~~~~~

原文地址:https://www.cnblogs.com/ishibin/p/2362602.html