当前位置:编程学习 > C#/ASP.NET >>

asp.net repeater 数字分页代码

asp教程.net repeater 数字分页代码
public static string sort { set; get; }
    private static string strsql;
    sqlconnection con = new sqlconnection("server=localhost;database=moldsystem;uid=sa;pwd=sa");//这个是全局变量
   
    private pageddatasource pds()
    {
        if (textbox4.text != "")
        {
            string a = textbox4.text;
            strsql = "select * from data_mold where customer_name='" + a + "'";
            dataset ds = new dataset();
            sqldataadapter sda = new sqldataadapter(strsql, con);
            sda.fill(ds, "name");
            pageddatasource pds = new pageddatasource();
            pds.datasource = ds.tables["name"].defaultview;
            pds.allowpaging = true;//允许分页
            pds.pagesize = 2;//单页显示项数
            pds.currentpageindex = convert.toint32(request.querystring["page"]);
            return pds;
        }
        else
        {
            strsql = "select * from data_mold";
            dataset ds = new dataset();
            sqldataadapter sda = new sqldataadapter(strsql, con);
            sda.fill(ds, "name");
            pageddatasource pds = new pageddatasource();
            pds.datasource = ds.tables["name"].defaultview;
            pds.allowpaging = true;//允许分页
            pds.pagesize = 2;//单页显示项数
            pds.currentpageindex = convert.toint32(request.querystring["page"]);
            return pds;
        }
    }
    protected void ddlp_selectedindexchanged(object sender, eventargs e)
    {//脚模板中的下拉列表框更改时激发
        string pg = convert.tostring((convert.toint32(((dropdownlist)sender).selectedvalue) - 1));//获取列表框当前选中项
        response.redirect("default2.aspx?page=" + pg);//页面转向
    }
protected void page_load(object sender, eventargs e)
    {
        if (!ispostback)
        {
            repeater1.datasource = pds();
            repeater1.databind();   
        }
    }
 protected void repeater1_itemcommand(object source, repeatercommandeventargs e)
    {

    }
    protected void repeater1_itemdatabound(object sender, repeateritemeventargs e)
    {
        if (e.item.itemtype == listitemtype.item || e.item.itemtype == listitemtype.alternatingitem)
        {
            for (int i = 0; i < e.item.controls.count; i++)
            {
                control control = e.item.controls[i];
                getcontrol(control);
            }
        }
        if (e.item.itemtype == listitemtype.footer)
        {
            dropdownlist ddlp = (dropdownlist)e.item.findcontrol("ddlp");

            hyperlink lpfirst = (hyperlink)e.item.findcontrol("hlfir");
            hyperlink lpprev = (hyperlink)e.item.findcontrol("hlp");
            hyperlink lpnext = (hyperlink)e.item.findcontrol("hln");
            hyperlink lplast = (hyperlink)e.item.findcontrol("hlla");

            pds().currentpageindex = ddlp.selectedindex;


            int n = convert.toint32(pds().pagecount);//n为分页数
            int i = convert.toint32(pds().currentpageindex);//i为当前页

            label lblpc = (label)e.item.findcontrol("lblpc");
            lblpc.text = n.tostring();
            label lblp = (label)e.item.findcontrol("lblp");
            lblp.text = convert.tostring(pds().currentpageindex + 1);

            if (!ispostback)
            {
                for (int j = 0; j < n; j++)
                {
                    ddlp.items.add(convert.tostring(j + 1));
                }
            }

            if (i <= 0)
            {
                lpfirst.enabled = false;
                lpprev.enabled = false;
                lplast.enabled = true;
                lpnext.enabled = true;
            }
            else
            {
                lpprev.navigateurl = "?page=" + (i - 1);
            }
            if (i >= n - 1)
            {
                lpfirst.enabled = true;
                lplast.enabled = false;
                lpnext.enabled = false;
                lpprev.enabled = true;
            }
            else
            {
                lpnext.navigateurl = "?page=" + (i + 1);
            }

            lpfirst.navigateurl = "?page=0";//向本页传递参数page
            lplast.navigateurl = "?page=" + (n - 1);

            ddlp.selectedindex = convert.toint32(pds().currentpageindex);//更新下拉列表框中的当前选中页序号
        }

    }
    protected void getcontrol(control control)
    {
        foreach (control c in control.controls)
        {

            if (c.hascontrols())
            {
                getcontrol(c);
            }
            else
            {
                if (c.gettype().tostring() == "system.web.ui.webcontrols.textbox")
                {
                    textbox txt = c as textbox;
                    txt.attributes.add("onclick", "write('" + txt.clientid + "');");//双击时是ondblclick
                    txt.attributes.add("onblur", "update('" + txt.clientid + "');");
                }
            }
        }
    }
    protected void button1_click(object sender, eventargs e)
    {
        repeater1.datasource = pds();
        repeater1.databind();
    }

补充:asp.net教程,.Net开发
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,