我用Datalist 做留言板,下面是我留言板 分页的简单代码
运行 后 Datalist 根本就没数据显示出来,这是为什么呢?
求大家指点,不胜感激
public void DataListBind()                                          //将数据绑定到Datalist控件
    {   
        string connectString = @"Data Source=.;Initial Catalog=MyWeb;User ID=sa;Password=123456";
        SqlConnection con = new SqlConnection(connectString);
        string sql = "select * from Message";
        SqlDataAdapter sda = new SqlDataAdapter(sql, con);
        DataSet ds = new DataSet();
        sda.Fill(ds,"no");
        PagedDataSource pds = new PagedDataSource();
        pds.DataSource = ds.Tables["no"].DefaultView;
        pds.AllowPaging=true;
        pds.PageSize = 10;
        this.DataList1.DataSource = pds;
        this.DataList1.DataBind();
    }
protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            DataListBind();
        }    }

解决方案 »

  1.   

    this.DataList1.DataSource = ds.Tables[0];
      this.DataList1.DataBind();
      

  2.   

    DataList中需要绑定相关字段,<%#Eval("字段名")%>
      

  3.   

    希望我的大卖你能满意:
    <% @ Import Namespace="System.Data" %>
    <% @ Import Namespace="System.Data.OleDb" %>
    <Script Language="C#" Runat="Server">OleDbConnection MyConn;
    int PageSize,RecordCount,PageCount,CurrentPage;
    public void Page_Load(Object src,EventArgs e)
    {
     //设定PageSize
     PageSize = 10; //连接语句
     string MyConnString = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source="+Server.MapPath(".")+"//App_Data\\1.mdb;";
     MyConn = new OleDbConnection(MyConnString);
     MyConn.Open(); //第一次请求执行
     if(!Page.IsPostBack)
     {
      ListBind();
      
      CurrentPage = 0;
      ViewState["PageIndex"] = 0;  //计算总共有多少记录
      RecordCount = CalculateRecord();
      lblRecordCount.Text = RecordCount.ToString();  //计算总共有多少页
      PageCount = RecordCount/PageSize;
      if(RecordCount%PageSize!=0)PageCount++;
      lblPageCount.Text = PageCount.ToString();
      ViewState["PageCount"] = PageCount;
      
      
     }
    }
    //计算总共有多少条记录
    public int CalculateRecord()
    {
     int intCount;
     string strCount = "select count(*) as co from hotel";
     OleDbCommand MyComm = new OleDbCommand(strCount,MyConn);
     OleDbDataReader dr = MyComm.ExecuteReader();
     if(dr.Read())
     {
      intCount = Int32.Parse(dr["co"].ToString());
     }
     else
     {
      intCount = 0;
     }
     dr.Close();
     return intCount;
    }ICollection CreateSource()
    { int StartIndex; //设定导入的起终地址
     StartIndex = CurrentPage*PageSize;
     string strSel = "select * from hotel";
     DataSet ds = new DataSet(); OleDbDataAdapter MyAdapter = new OleDbDataAdapter(strSel,MyConn);
     MyAdapter.Fill(ds,StartIndex,PageSize,"Score"); return ds.Tables["Score"].DefaultView;
    }
    public void ListBind()
    {
     score.DataSource = CreateSource();
     score.DataBind(); lbnNextPage.Enabled = true;
     lbnPrevPage.Enabled = true;
     shouye.Enabled= true;
     moye.Enabled= true;
     if(CurrentPage==(PageCount-1))
      {
     lbnNextPage.Enabled = false;
     moye.Enabled= false;
     }
     if(CurrentPage==0) 
     {
     lbnPrevPage.Enabled = false;
     shouye.Enabled =false;
     }
     lblCurrentPage.Text = (CurrentPage+1).ToString();}public void Page_OnClick(Object sender,CommandEventArgs e)
    {
     CurrentPage = (int)ViewState["PageIndex"];
     PageCount = (int)ViewState["PageCount"]; string cmd = e.CommandName;
     //判断cmd,以判定翻页方向
     switch(cmd)
     {
      case "next":
       if(CurrentPage<(PageCount-1)) CurrentPage++;
       break;
      case "prev":
       if(CurrentPage>0) CurrentPage--;
       break;
       case "shou":
          CurrentPage=0 ;
          break;
       case "mo":
          CurrentPage=PageCount-1;
          break;
     } ViewState["PageIndex"] = CurrentPage; ListBind();}
    </script>
    <html>
    <head>
    <title>datalist分页效果</title>
    <link href="admin.css" type="text/css" />
    </head>
    <body>
    <form id="Form1" runat="server">
        &nbsp;<table style="width: 680px">
            <tr>
                <td style="width: 680px" align="center">
    共有<asp:Label id="lblRecordCount" ForeColor="red" runat="server" />条记录&nbsp;
    当前为<asp:Label id="lblCurrentPage" ForeColor="red" runat="server" />/<asp:Label id="lblPageCount" ForeColor="red" runat="server" />页&nbsp;
                </td>
            </tr>
            <tr>
                <td style="width: 680px; height: 218px" align="center"><asp:DataList id="score" runat="server"
    HeaderStyle-BackColor="#aaaadd"
    AlternatingItemStyle-BackColor="Gainsboro"
    EditItemStyle-BackColor="yellow" BackColor="White" BorderColor="#CC9966" BorderStyle="None" BorderWidth="1px" CellPadding="4" GridLines="Both"
    >
     <ItemTemplate>
     <table><tr><td  width="200px">
      酒店名称:<%# DataBinder.Eval(Container.DataItem,"Name") %>  </td><td width="120px">星级:<%# DataBinder.Eval(Container.DataItem,"satr") %> </td><td>城市:<%# DataBinder.Eval(Container.DataItem,"City") %> </td></tr></table>   
      
     </ItemTemplate>
        <FooterStyle BackColor="#FFFFCC" ForeColor="#330099" />
        <EditItemStyle BackColor="Yellow" />
        <SelectedItemStyle BackColor="#FFCC66" Font-Bold="True" ForeColor="#663399" />
        <ItemStyle BackColor="White" ForeColor="#330099" />
        <HeaderStyle BackColor="#990000" Font-Bold="True" ForeColor="#FFFFCC" />
        <AlternatingItemStyle BackColor="Gainsboro" />
        <HeaderTemplate>
            黄山酒店一揽表
        
            
        </HeaderTemplate>
        <FooterTemplate>
            &nbsp;&nbsp;
        </FooterTemplate>
    </asp:DataList></td>
            </tr>
            <tr>
                <td style="width: 680px" align="center">
        <asp:LinkButton ID="shouye" runat="server" Text="首页" CommandName="shou" OnCommand="Page_OnClick"/>
      
    <asp:LinkButton id="lbnPrevPage" Text="上一页" CommandName="prev" OnCommand="Page_OnClick" runat="server" />
    <asp:LinkButton id="lbnNextPage" Text="下一页" CommandName="next" OnCommand="Page_OnClick" runat="server" />
      <asp:LinkButton ID="moye" runat="server"  Text="末页"   CommandName="mo" OnCommand="Page_OnClick"/></td>
            </tr>
        </table>
        &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;</form>
    </body>
    </html>
      

  4.   

    希望我的大卖你能满意:
    <% @ Import Namespace="System.Data" %>
    <% @ Import Namespace="System.Data.OleDb" %>
    <Script Language="C#" Runat="Server">OleDbConnection MyConn;
    int PageSize,RecordCount,PageCount,CurrentPage;
    public void Page_Load(Object src,EventArgs e)
    {
     //设定PageSize
     PageSize = 10; //连接语句
     string MyConnString = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source="+Server.MapPath(".")+"//App_Data\\1.mdb;";
     MyConn = new OleDbConnection(MyConnString);
     MyConn.Open(); //第一次请求执行
     if(!Page.IsPostBack)
     {
      ListBind();
      
      CurrentPage = 0;
      ViewState["PageIndex"] = 0;  //计算总共有多少记录
      RecordCount = CalculateRecord();
      lblRecordCount.Text = RecordCount.ToString();  //计算总共有多少页
      PageCount = RecordCount/PageSize;
      if(RecordCount%PageSize!=0)PageCount++;
      lblPageCount.Text = PageCount.ToString();
      ViewState["PageCount"] = PageCount;
      
      
     }
    }
    //计算总共有多少条记录
    public int CalculateRecord()
    {
     int intCount;
     string strCount = "select count(*) as co from hotel";
     OleDbCommand MyComm = new OleDbCommand(strCount,MyConn);
     OleDbDataReader dr = MyComm.ExecuteReader();
     if(dr.Read())
     {
      intCount = Int32.Parse(dr["co"].ToString());
     }
     else
     {
      intCount = 0;
     }
     dr.Close();
     return intCount;
    }ICollection CreateSource()
    { int StartIndex; //设定导入的起终地址
     StartIndex = CurrentPage*PageSize;
     string strSel = "select * from hotel";
     DataSet ds = new DataSet(); OleDbDataAdapter MyAdapter = new OleDbDataAdapter(strSel,MyConn);
     MyAdapter.Fill(ds,StartIndex,PageSize,"Score"); return ds.Tables["Score"].DefaultView;
    }
    public void ListBind()
    {
     score.DataSource = CreateSource();
     score.DataBind(); lbnNextPage.Enabled = true;
     lbnPrevPage.Enabled = true;
     shouye.Enabled= true;
     moye.Enabled= true;
     if(CurrentPage==(PageCount-1))
      {
     lbnNextPage.Enabled = false;
     moye.Enabled= false;
     }
     if(CurrentPage==0) 
     {
     lbnPrevPage.Enabled = false;
     shouye.Enabled =false;
     }
     lblCurrentPage.Text = (CurrentPage+1).ToString();}public void Page_OnClick(Object sender,CommandEventArgs e)
    {
     CurrentPage = (int)ViewState["PageIndex"];
     PageCount = (int)ViewState["PageCount"]; string cmd = e.CommandName;
     //判断cmd,以判定翻页方向
     switch(cmd)
     {
      case "next":
       if(CurrentPage<(PageCount-1)) CurrentPage++;
       break;
      case "prev":
       if(CurrentPage>0) CurrentPage--;
       break;
       case "shou":
          CurrentPage=0 ;
          break;
       case "mo":
          CurrentPage=PageCount-1;
          break;
     } ViewState["PageIndex"] = CurrentPage; ListBind();}
    </script>
    <html>
    <head>
    <title>datalist分页效果</title>
    <link href="admin.css" type="text/css" />
    </head>
    <body>
    <form id="Form1" runat="server">
        &nbsp;<table style="width: 680px">
            <tr>
                <td style="width: 680px" align="center">
    共有<asp:Label id="lblRecordCount" ForeColor="red" runat="server" />条记录&nbsp;
    当前为<asp:Label id="lblCurrentPage" ForeColor="red" runat="server" />/<asp:Label id="lblPageCount" ForeColor="red" runat="server" />页&nbsp;
                </td>
            </tr>
            <tr>
                <td style="width: 680px; height: 218px" align="center"><asp:DataList id="score" runat="server"
    HeaderStyle-BackColor="#aaaadd"
    AlternatingItemStyle-BackColor="Gainsboro"
    EditItemStyle-BackColor="yellow" BackColor="White" BorderColor="#CC9966" BorderStyle="None" BorderWidth="1px" CellPadding="4" GridLines="Both"
    >
     <ItemTemplate>
     <table><tr><td  width="200px">
      酒店名称:<%# DataBinder.Eval(Container.DataItem,"Name") %>  </td><td width="120px">星级:<%# DataBinder.Eval(Container.DataItem,"satr") %> </td><td>城市:<%# DataBinder.Eval(Container.DataItem,"City") %> </td></tr></table>   
      
     </ItemTemplate>
        <FooterStyle BackColor="#FFFFCC" ForeColor="#330099" />
        <EditItemStyle BackColor="Yellow" />
        <SelectedItemStyle BackColor="#FFCC66" Font-Bold="True" ForeColor="#663399" />
        <ItemStyle BackColor="White" ForeColor="#330099" />
        <HeaderStyle BackColor="#990000" Font-Bold="True" ForeColor="#FFFFCC" />
        <AlternatingItemStyle BackColor="Gainsboro" />
        <HeaderTemplate>
            黄山酒店一揽表
        
            
        </HeaderTemplate>
        <FooterTemplate>
            &nbsp;&nbsp;
        </FooterTemplate>
    </asp:DataList></td>
            </tr>
            <tr>
                <td style="width: 680px" align="center">
        <asp:LinkButton ID="shouye" runat="server" Text="首页" CommandName="shou" OnCommand="Page_OnClick"/>
      
    <asp:LinkButton id="lbnPrevPage" Text="上一页" CommandName="prev" OnCommand="Page_OnClick" runat="server" />
    <asp:LinkButton id="lbnNextPage" Text="下一页" CommandName="next" OnCommand="Page_OnClick" runat="server" />
      <asp:LinkButton ID="moye" runat="server"  Text="末页"   CommandName="mo" OnCommand="Page_OnClick"/></td>
            </tr>
        </table>
        &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;</form>
    </body>
    </html>
      

  5.   

    如果这个方法你不能理解的话,我建议你用我常用的一种方法:
    用PageDataSource绑定数据源也行
      

  6.   

    aspnetpager分页控件,简单方便快捷,连示例都有
    http://www.webdiyer.com/Controls/AspNetPager/Downloads
      

  7.   

    DataList没有分页功能,必须用代码实现
      

  8.   

    谢谢大家,经过一天的专研已经弄出来了,DataList要前台写模板 添加字段
    这个帖子解决了我的问题
    http://topic.csdn.net/u/20090318/12/f09965c7-dcbc-4bb3-a0a3-3a8c267e49e0.html
      

  9.   

    谢谢大家,经过一天的专研已经弄出来了,DataList要前台写模板 添加字段
    这个帖子解决了我的问题
    http://topic.csdn.net/u/20090318/12/f09965c7-dcbc-4bb3-a0a3-3a8c267e49e0.html