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

这样绑定Datalist后,分页怎么弄啊!!!高手进~~~

后台代码:

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using SuperMan;
using DAL.Brand;

public partial class Brand_Default : System.Web.UI.Page
{
    SqlHelper sql = new SqlHelper();
    Brand brand = new Brand();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            BindBrand();
        }
    }


    private void BindBrand()
    {
        DataSet ds = brand.BrandListNum();
        Dl_Brand.DataSource = ds;
        Dl_Brand.DataBind();
        if (ds.Tables[0].Rows.Count > 0)
        {
            for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
            {
                HyperLink hy1 = (HyperLink)Dl_Brand.Items[i].FindControl("Hy_Title");
                ImageButton logo = (ImageButton)Dl_Brand.Items[i].FindControl("Img_Btn_Logo");
                hy1.Text = ds.Tables[0].Rows[i][1].ToString();
                hy1.NavigateUrl = "BrandView.aspx?BrandId=" + ds.Tables[0].Rows[i][0].ToString();
                //获取图片地址
                brand.BrandTypeId = Convert.ToInt32(ds.Tables[0].Rows[i][0].ToString());
                logo.ImageUrl = "~/"+brand.GetBrandLogoUrl();
                logo.PostBackUrl = hy1.NavigateUrl.ToString();
            }
        }
        else
        {
            Dl_Brand.ShowHeader = true;
        }

    }
    private void PageSwich()
    {
        

    }
}

后台绑定字段后,怎么加入分页????

高手进~ 多谢啊~~~ --------------------编程问答-------------------- 可用aspnetpager控件分页,免费的效果不错! --------------------编程问答-------------------- 自己写个吧,练习下, --------------------编程问答-------------------- 我找了好多方法 都报错,原因可能是,我先设置数据源,然后又在后台代码中绑定字段后出错:

--------------------编程问答-------------------- 初学者用自带的pageDataSource...练手试试.. --------------------编程问答-------------------- DataSet ds = brand.BrandListNum();
        PagedDataSource objPds = new PagedDataSource();
        objPds.DataSource = ds;
        objPds.AllowPaging = true;
        objPds.PageSize = 10;
        int CurPage;
        //double temp = dt.Rows.Count / objPds.PageSize; 
        int PageCount = dtApp.Rows.Count / objPds.PageSize;
        if (dtApp.Rows.Count % objPds.PageSize > 0)
        {
            PageCount++;
        }

        if (Request.QueryString["Page"] != null)
        {
            CurPage = Convert.ToInt32(Request.QueryString["Page"]);
        }
        else
        {
            CurPage = 1;
        }

        objPds.CurrentPageIndex = CurPage - 1;
        lblCurrentPage.Text = "当前页:" + CurPage.ToString() + "  共" + PageCount.ToString() + "页";
        LnkFirst.NavigateUrl = Request.CurrentExecutionFilePath;
        if (!objPds.IsFirstPage)
            lnkPrev.NavigateUrl = Request.CurrentExecutionFilePath + "?Page=" + Convert.ToString(CurPage - 1);

        if (!objPds.IsLastPage)
            lnkNext.NavigateUrl = Request.CurrentExecutionFilePath + "?Page=" + Convert.ToString(CurPage + 1);

        LnkLast.NavigateUrl = Request.CurrentExecutionFilePath + "?Page=" + Convert.ToString(PageCount);
        Dl_Brand.DataSource = objPds;
        Dl_Brand.DataBind();
        if (ds.Tables[0].Rows.Count > 0)
        {
            for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
            {
                HyperLink hy1 = (HyperLink)Dl_Brand.Items[i].FindControl("Hy_Title");
                ImageButton logo = (ImageButton)Dl_Brand.Items[i].FindControl("Img_Btn_Logo");
                hy1.Text = ds.Tables[0].Rows[i][1].ToString();
                hy1.NavigateUrl = "BrandView.aspx?BrandId=" + ds.Tables[0].Rows[i][0].ToString();
                //获取图片地址
                brand.BrandTypeId = Convert.ToInt32(ds.Tables[0].Rows[i][0].ToString());
                logo.ImageUrl = "~/" + brand.GetBrandLogoUrl();
                logo.PostBackUrl = hy1.NavigateUrl.ToString();
            }
        }
--------------------编程问答-------------------- 这个好像也不行啊 关键问题是,我先绑定数据源后又在cs里面绑定datalist模板列中的字段问题 --------------------编程问答-------------------- //if (ds.Tables[0].Rows.Count > 0)
        //{
        //    for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
        //    {
        //        HyperLink hy1 = (HyperLink)Dl_Brand.Items[i].FindControl("Hy_Title");
        //        ImageButton logo = (ImageButton)Dl_Brand.Items[i].FindControl("Img_Btn_Logo");
        //        hy1.Text = ds.Tables[0].Rows[i][1].ToString();
        //        hy1.NavigateUrl = "BrandView.aspx?BrandId=" + ds.Tables[0].Rows[i][0].ToString();
        //        //获取图片地址
        //        brand.BrandTypeId = Convert.ToInt32(ds.Tables[0].Rows[i][0].ToString());
        //        logo.ImageUrl = "~/" + brand.GetBrandLogoUrl();
        //        logo.PostBackUrl = hy1.NavigateUrl.ToString();
        //    }
        //}


上面的代码我注销掉这些 可以正常分页,在cs里面绑定数据到页面控件就出错.

在页面里面用Evla("字段")就不会出错 天!!!!


怎么解决 那位大仙 知道???? --------------------编程问答-------------------- 很明显,楼主应该单步调试一下,就明白了,分页的时候,你的Datatable变量值会不会变呢? --------------------编程问答-------------------- 单步调试过了 数据绑定先后问题 怎么解决啊? --------------------编程问答-------------------- Cs代码:
=====================================================================
using System;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class DataList_Pageing2 : System.Web.UI.Page
{
    SqlConnection con;
    SqlDataAdapter da;
    DataSet ds;

    public int Pagesize
    {
        get
        {
            return Convert.ToInt32(Session["page"]);
        }
        set
        {
            Session["page"] = value;
        }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            int n=0;
            if (Request.QueryString["page"] != "")
            {
                n = Convert.ToInt32(Request.QueryString["page"]);
            }
            else
            {
                n = 0;
            }
            Bind(n);
        }
    }

    public void Bind(int n)
    {
        this.DataList1.DataSource = pds(n);
        this.DataList1.DataBind();
    }

    public DataSet GetData(string sql)
    {
        con = new SqlConnection("server=.;uid=sa;pwd=;database=pubs");
        da = new SqlDataAdapter(sql,con);
        ds = new DataSet();
        da.Fill(ds);
        return ds;
    }

    public PagedDataSource pds()
    {
        PagedDataSource pds = new PagedDataSource();
        pds.DataSource = GetData("select * from authors").Tables[0].DefaultView;
        pds.AllowPaging = true;
        pds.PageSize = Pagesize;
        return pds;
    }

    public PagedDataSource pds(int n)
    {
        PagedDataSource pds = new PagedDataSource();
        pds.DataSource = GetData("select * from authors").Tables[0].DefaultView;
        pds.AllowPaging = true;
        pds.PageSize = Pagesize;
        pds.CurrentPageIndex = n;
        return pds;
    }
    protected void DataList1_ItemDataBound(object sender, DataListItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.Footer)
        {
            PlaceHolder ph = (PlaceHolder)e.Item.FindControl("ph");
            for (int i = 0; i < pds().PageCount; i++)
            {
                HyperLink hp = new HyperLink();
                Literal li = new Literal();

                int n = i + 1;
                hp.ID = n.ToString();
                hp.Text = n.ToString();
                hp.NavigateUrl = "?page=" + i.ToString();
                li.Text = " ";
                ph.Controls.Add(hp);
                ph.Controls.Add(li);
            }
        }
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        Session["page"] = Convert.ToInt32(TextBox1.Text);
        Bind(0);
        string str = Request.Url.ToString().Substring(0,21);
        Response.Write(str);
    }
}
--------------------编程问答-------------------- HTML代码:
============================================
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>无标题页</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:DataList ID="DataList1" runat="server" Style="position: relative" RepeatDirection="Horizontal" CellPadding="4" ForeColor="#333333" OnItemDataBound="DataList1_ItemDataBound">
            <ItemTemplate>
        <table style="width: 100px; position: relative">
            <tr>
                <td>
                    <asp:Label ID="lblauid" runat="server" Text='<%# DataBinder.Eval(Container.DataItem,"au_id") %>'></asp:Label></td>
            </tr>
            <tr>
                <td>
                    <asp:Label ID="lblaufname" runat="server" Text='<%# DataBinder.Eval(Container.DataItem,"au_fname") %>'></asp:Label></td>
            </tr>
            <tr>
                <td>
                    <asp:Label ID="lblaucity" runat="server" Text='<%# DataBinder.Eval(Container.DataItem,"city") %>'></asp:Label></td>
            </tr>
            <tr>
                <td>
                    <asp:Label ID="lblauzip" runat="server" Text='<%# DataBinder.Eval(Container.DataItem,"zip") %>'></asp:Label></td>
            </tr>
        </table>
            </ItemTemplate>
            <FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
            <SelectedItemStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
            <AlternatingItemStyle BackColor="White" />
            <ItemStyle BackColor="#EFF3FB" />
            <HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
            <FooterTemplate>
                <asp:PlaceHolder ID="ph" runat="server"></asp:PlaceHolder>
            </FooterTemplate>
        </asp:DataList></div>
        <asp:TextBox ID="TextBox1" runat="server" Style="position: relative"></asp:TextBox>
        <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Style="position: relative"
            Text="Button" />
    </form>
</body>
</html>
--------------------编程问答-------------------- 沾过去直接用 --------------------编程问答-------------------- 用存储过程分页比较方便吧!! --------------------编程问答-------------------- 我弄了一个存储过程分页的,可以了,但是,代码很多.我在尝试将分页的功能集成在.ascx里面 ,这样那个页面需要分页,直接拖过去,设置一下就可以了,,大家谁弄过,分页继承在ascx用户控件里面的??? --------------------编程问答-------------------- 大师们谁做过上面提得? --------------------编程问答-------------------- 顶一顶~~~ --------------------编程问答-------------------- ASP.NET2.0中datalist仿百度分页 
注意这个分页在1.0中无效 
using System; 
using System.Data; 
using System.Configuration; 
using System.Collections; 
using System.Web; 
using System.Web.Security; 
using System.Web.UI; 
using System.Web.UI.WebControls; 
using System.Web.UI.WebControls.WebParts; 
using System.Web.UI.HtmlControls; 
using MySql.Data.MySqlClient; 
using System.Data.SqlClient; 
using System.IO; 
public partial class mysql : System.Web.UI.Page 

    SqlConnection conn = new SqlConnection(System.Configuration.ConfigurationManager.AppSettings["constrmy"]); 
    int ToatalCountRecord;//总记录数 
    int PageItem = 4;//每页显示的条数 
    int CurrentPage = 1;//当前页数 
    protected void Page_Load(object sender, EventArgs e) 
    { 
        
        if (!this.Page.IsPostBack) 
        { 
            if (Request.QueryString["page"] != null) 
            { 
                if (!Int32.TryParse(Request.QueryString["page"].ToString(), out CurrentPage)) 
                { 
                    Response.Write("请输入分页参数!"); 
                    Response.End(); 
                    return; 
                } 
            } 
            
            this.BuidGrid(); 
        } 
    } 
    
    private void BuidGrid() 
    { 
        string s2 = "select * from ts1"; 
        SqlDataAdapter da = new SqlDataAdapter(s2, conn); 
        DataSet ds = new DataSet(); 
        int startRecord = (CurrentPage - 1) * PageItem; 
        da.Fill(ds, startRecord, PageItem, "a"); 
        this.DataList1.DataSource = ds.Tables["a"].DefaultView; 
        this.DataList1.DataBind(); 
        SqlCommand comm = new SqlCommand("select count(*) from ts1", conn); 
        conn.Open(); 
        ToatalCountRecord = Convert.ToInt32(comm.ExecuteScalar()); 
        conn.Close(); 
        BuildPages(); 
    } 
    private void BuildPages() 
    { 
        int Step = 5;//偏移量 
        int LeftNum = 0;//做界限 
        int RightNum = 0;//右界限 
        string PageUrl = Request.FilePath; 
        int PageCount = (int)Math.Ceiling((double)(ToatalCountRecord) / PageItem); 
        if (CurrentPage - Step  < 1) 
        { 
            LeftNum = 1; 
        } 
        else 
        { 
            LeftNum = CurrentPage - Step; 
        } 
        if (CurrentPage + Step > PageCount) 
        { 
            RightNum = PageCount; 
        } 
        else 
        { 
            RightNum = CurrentPage + Step; 
        } 
        string OutPut = ""; 
        if (CurrentPage > 1) 
        { 
            OutPut += " <a href='" + PageUrl + "?page=" + (CurrentPage - 1) + "'>" + "上一页" + " </a>"; 
        } 
        for (int i = LeftNum; i  <= RightNum; i++) 
        { 
            if (i == CurrentPage) 
            { 
                OutPut += " <font color=red>" + " " +"["+i.ToString() +"]"+ "" + " </font>"; 
            } 
            else 
            { 
                OutPut += " <a href='" + PageUrl + "?page=" + i.ToString() + "'>" + " " +"["+ i.ToString() +"]"+ " " + " </a>"; 
            } 
        } 
        if (CurrentPage  < PageCount) 
        { 
            OutPut += " <a href='" + PageUrl + "?page=" + (CurrentPage + 1) + "'>" + "下一页" + " </a>"; 
        } 
        this.PageInfo.InnerHtml = OutPut; 
    } 
  

请注意颜色标注部分 
需要在前台添加一个  <div id="PageInfo" runat="server" > --------------------编程问答-------------------- aspnetpager
或者
pagedatasource
补充:.NET技术 ,  ASP.NET
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,