日期:2014-05-17  浏览次数:20455 次

datalist分页只显示一页的问题
源码:
C# code

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 MySql.Data.MySqlClient;

public partial class SourceList : System.Web.UI.Page
{
    DB db = new DB();
    string source_type,tag;
    PagedDataSource pds;
    DataView dview;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)  //首次加载页面
        {
            try
            {
                source_type = db.ConverSql(Request["SourceType"].ToString());
                tag = db.ConverSql(Request["Tag"].ToString());
            }
            catch (Exception ex)
            {
                Response.Redirect("~/Default.aspx");
            }
            ViewState["pageindex"] = "0";
            InitData();
        }
        
    }
    /*
     *显示界面上的资源列表 
     * 
     */
    public void InitData()
    {
        string strsql = "";
        pds = new PagedDataSource();
        pds.PageSize = 2;
        pds.AllowPaging = true;
        Title = source_type + "|" + tag + "下载列表";
        strsql = "select * from tb_SourceInfo where SourceType ='" + source_type + "' and Tag='" + tag + "'";
        MySqlCommand mycmd = db.GetCommandStr(strsql);
        try
        {
            db.ExecNonQuery(mycmd);
        }
        catch (Exception ex)
        {
            Response.Redirect("~/Default.aspx");
        }
        DataTable dtTable = db.GetDataSet(mycmd, "news");

        pds.CurrentPageIndex = int.Parse(ViewState["pageindex"].ToString());

        if (dtTable != null)  //判断dsTable里面是否存在数据
        {
            dview = dtTable.DefaultView;
            pds.DataSource = dview;
            this.SouceList.DataSource = pds;
            this.SouceList.DataBind();
            if (!pds.IsFirstPage)
            {
                lkPre.Visible = true;
            }
            else
            {
                lkPre.Visible = false;
            }
            if (!pds.IsLastPage)    
            {
                lkNext.Visible = true;
            }
            else
            {
                lkNext.Visible = false;
            }
            this.page_num.InnerText = Convert.ToString(pds.CurrentPageIndex + 1);
            this.page_num_total.InnerText = "共" + pds.PageCount.ToString() + "页";
        }
        else  //不存在数据
        {
            //this.td_content.InnerHtml = "<div>没有内容了</div>";
            lkPre.Visible = false;
            lkPre.Visible = false;
        }

    }
    protected void IndexChanging(object sender, CommandEventArgs e)
    {
        string strCommand = ((LinkButton)sender).CommandArgument.ToString();
        int pageindex = int.Parse(ViewState["pageindex"].ToString());
        switch (strCommand)
        {
            case "pre":
                pageindex = pageindex - 1;
                break;
            case "next":
                pageindex = pageindex + 1;
                break;
        }
        ViewState["pageindex"] = pageindex;
        InitData();
    }
}


前台:
HTML code

<div style="float:left;width:70%;margin-left:2PX;border:1px solid #9BD;">
    <asp:DataList ID="SouceList" runat="server" Width="700px">
        <ItemTemplate>
            <div style="font-size:20px;"><a href="download.aspx?SourceID=<%# DataBinder.Eval(Container.DataItem, "SourceID").ToString() %>" class="list_a">