http://www.aspxcn.com/dotnetbbs/View.aspx?fbId=17&Id=225005

解决方案 »

  1.   

    CurrentPage=(int)ViewState["StartIndex"];ViewState["StartIndex"]定义在哪了?
      

  2.   

    我找了很多例子都没有这个ViewState["StartIndex"],搞了半天都搞不好!
    请帮忙对其进行改正。
      

  3.   

    太麻烦了,用这个吧:http://www.webdiyer.com
      

  4.   

    第九行ViewState["CurrentPage"]=0;换成ViewState["PageIndex"] = 0; 
    第十一行CurrentPage=(int)ViewState["CurrentPage"];换成CurrentPage = (int)ViewState["PageIndex"]; 
    试试看
      

  5.   

    第三十一行ViewState["CurrentPage"]=CurrentPage;
    换成ViewState["PageIndex"] = CurrentPage;
      

  6.   

    <%@ Import Namespace="System.Data.OleDb"%>
    <%@ Import Namespace="System.Data"%>
    <HTML><HEAD><script language="C#" runat="server">
    int PageSize, RecordCount,PageCount,CurrentPage;
    public void Page_Load(object sender, System.EventArgs e){
    PageSize=10;
    if (!IsPostBack){
                               ViewState["CurrentPage"]=0;
                                        BindGrid();
                                        }
                                        }

    void PagerButtonClick(object sender,CommandEventArgs e){
           PageCount=(int)ViewState["PageCount"];
    //获得LinkButton的参数值
    string arg=e.CommandName;
    switch(arg)
    {case "next":
     if((int)ViewState["CurrentPage"]<(PageCount-1))
    ViewState["CurrentPage"]=ViewState["CurrentPage"]+1;
    break;
    case "prev":
    if((int)ViewState["CurrentPage"]>0)
    CurrentPage--;
    break;
    case "last":
    if((int)ViewState["CurrentPage"]<(PageCount-1));
    ViewState["CurrentPage"]=(PageCount-1);
    break;
    case "first" :
    ViewState["CurrentPage"]=0;
    break;}
    BindGrid();
    }
       ICollection CreateDataSource(){ 
              int StartIndex;
              StartIndex=(int)ViewState(CurrentPage)*PageSize;
              CurrentPage=(int)ViewState["CurrentPage"];
    string connstring=@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Inetpub\wwwroot\Ch0754\ch0754.mdb;Persist Security Info=False";
      string sql="select rowid,title,msgbody from CH0754 ";
      OleDbConnection conn=new OleDbConnection(connstring);
      OleDbDataAdapter da=new OleDbDataAdapter(sql,conn);
      DataSet ds=new DataSet();
      da.Fill(ds,(int)StartIndex,(int)PageSize,"try");
      return ds.Tables["try"].DefaultView;

    public void BindGrid(){
     DataList1.DataSource=CreateDataSource();
     DataList1.DataBind();
                     }
    </script>
    </HEAD>
    <body MS_POSITIONING="GridLayout">
    <form id="Form1" method="post" runat="server">
    <FONT face="宋体">
    <asp:datalist id="DataList1" runat="server">
    <ItemTemplate>
    <%# DataBinder.Eval(Container.DataItem,"rowid")%>
    <%# DataBinder.Eval(Container.DataItem,"title")%>
    </ItemTemplate>
    </asp:datalist></FONT>
    <P>
    <asp:LinkButton id="btnFirst" runat="server" Text="首页" CommandName="first" ForeColor="navy" Font-Name="verdana"
    Font-size="8pt" OnCommand="PagerButtonClick"></asp:LinkButton>
    <asp:LinkButton id="btnPrev" runat="server" Text="前页" CommandName="prev" ForeColor="navy" Font-Name="verdana"
    Font-size="8pt" OnCommand="PagerButtonClick"></asp:LinkButton>
    <asp:LinkButton id="btnNext" runat="server" Text="后页" CommandName="next" ForeColor="navy" Font-Name="verdana"
    Font-size="8pt" OnCommand="PagerButtonClick"></asp:LinkButton>
    <asp:LinkButton id="btnLast" runat="server" Text="末页" CommandName="last" ForeColor="navy" Font-Name="verdana"
    Font-size="8pt" OnCommand="PagerButtonClick"></asp:LinkButton></P>
    </form>
    </body>
    </HTML>
      

  7.   

    case "prev":
    if((int)ViewState["CurrentPage"]>0)
    ((int)ViewState["CurrentPage"]--;
    break;
      

  8.   

    另外你的PageCount需要另外计算出来
      

  9.   

    改了很多,还是错误,我想应该是那个PageIndex出的错。我在网上找了很多这样的例子,其中多是大同小异,就是没有对PageIndex的处理。谁能说说啊!或者给个调试通过的。
      

  10.   

    using System;
    using System.Collections;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Web;
    using System.Web.SessionState;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.HtmlControls;
    using System.Configuration;
    using System.Web.Security;
    using System.Data.SqlClient;namespace wshd.pricenews
    {
    /// <summary>
    /// priceshow 的摘要说明。
    /// </summary>
    public class priceshow : System.Web.UI.Page
    {
    protected System.Web.UI.WebControls.LinkButton first;
    protected System.Web.UI.WebControls.LinkButton prev;
    protected System.Web.UI.WebControls.LinkButton next;
    protected System.Web.UI.WebControls.LinkButton last;
    protected System.Web.UI.WebControls.Label lblallcount;
    protected System.Web.UI.WebControls.Label lblpagecount;
    protected System.Web.UI.WebControls.Label lblcurrentindex;
    protected System.Web.UI.WebControls.DataList DataList1;
    protected System.Web.UI.WebControls.Button go;
        int PageSize,RecordCount,PageCount,CurrentPage;
    protected System.Web.UI.WebControls.TextBox to;

    private void Page_Load(object sender, System.EventArgs e)
    {
    // 在此处放置用户代码以初始化页面
    first.Text="首页";
    prev.Text="前一页";
    next.Text="下一页";
    last.Text="尾页";
    //分页分十页
    PageSize = 2; 
    if(!IsPostBack)
    {
    CurrentPage = 0; 
    ViewState["PageIndex"] = 0; 
    //datalistkb();
    ListBind(); }



    //计算总共有多少记录 
     RecordCount = CalculateRecord(); 
    lblallcount.Text = RecordCount.ToString();
    //计算总共有多少页 
    PageCount = RecordCount/PageSize; 
    lblpagecount.Text = PageCount.ToString(); 
    ViewState["PageCount"] = PageCount; 
    } #region Web Form Designer generated code
    override protected void OnInit(EventArgs e)
    {
    //
    // CODEGEN:该调用是 ASP.NET Web 窗体设计器所必需的。
    //
    InitializeComponent();
    base.OnInit(e);
    }

    /// <summary>
    /// 设计器支持所需的方法 - 不要使用代码编辑器修改
    /// 此方法的内容。
    /// </summary>
    private void InitializeComponent()
    {    
    this.first.Click += new System.EventHandler(this.btnClick);
    this.prev.Click += new System.EventHandler(this.btnClick);
    this.next.Click += new System.EventHandler(this.btnClick);
    this.last.Click += new System.EventHandler(this.btnClick);
    this.go.Click += new System.EventHandler(this.go_Click);
    this.Load += new System.EventHandler(this.Page_Load); }
    #endregion
    private void datalistkb()
    {

    SqlConnection cn=new SqlConnection(ConfigurationSettings.AppSettings["connectionString"]);
    String strSQL="select * from pricenews";

    SqlCommand cm=new SqlCommand(strSQL,cn);
    // cm.Parameters.Add(new SqlParameter("@ID",SqlDbType.Int,10));
    // cm.Parameters["@ID"].Value=Request.QueryString["ID"];
    SqlDataAdapter da=new SqlDataAdapter(cm);
    DataSet ds=new DataSet();
    da.Fill(ds);
    DataList1.DataSource=ds;
    DataList1.DataBind();

    }
    //计算总共有多少条记录 
    public int CalculateRecord() 

    SqlConnection cn=new SqlConnection(ConfigurationSettings.AppSettings["connectionString"]);
    int intCount;
    cn.Open();
    string strCount = "select count(*) as co from pricenews"; 
    SqlCommand cm = new SqlCommand(strCount,cn); 
    SqlDataReader dr = cm.ExecuteReader(); 
    if(dr.Read()) 

    intCount = Int32.Parse(dr["co"].ToString()); 

    else 

    intCount = 0; 

    cn.Close(); 
    return intCount; 
    } private void btnClick(object sender, System.EventArgs e)
    {
    CurrentPage = (int)ViewState["PageIndex"]; 
    PageCount = (int)ViewState["PageCount"]; 
                 string cmd=((LinkButton)sender).CommandArgument;
    // string cmd = e.CommandArgument; 
    //判断cmd,以判定翻页方向 
    switch(cmd) 

    case "first":
    CurrentPage=0;
    break;
    case "next": 
    if(CurrentPage<(PageCount-1)) CurrentPage++; 
    break; 
    case "prev": 
    if(CurrentPage>0)
    CurrentPage--; 
    break; 
        case "last":
    CurrentPage=PageCount-1;
    break;

       
    ViewState["PageIndex"] = CurrentPage; 
    ListBind();
        } 
    ICollection CreateSource()
    { int StartIndex;
                SqlConnection cn=new SqlConnection(ConfigurationSettings.AppSettings["connectionString"]);
    //设定导入的起终地址
    StartIndex = CurrentPage*PageSize;
    string strSel = "select * from pricenews";
    DataSet ds = new DataSet(); SqlDataAdapter da= new SqlDataAdapter(strSel,cn);
    da.Fill(ds,StartIndex,PageSize,"pricenews"); return ds.Tables["pricenews"].DefaultView;
    }
    public void ListBind() 

    DataList1.DataSource = CreateSource(); 
    DataList1.DataBind(); 
       
    // lbnNextPage.Enabled = true; 
    // lbnPrevPage.Enabled = true; 
    // if(CurrentPage==(PageCount-1)) lbnNextPage.Enabled = false; 
    // if(CurrentPage==0) lbnPrevPage.Enabled = false; 
    lblcurrentindex.Text = (CurrentPage+1).ToString(); 
       
    } private void go_Click(object sender, System.EventArgs e)
    {

    if(Convert.ToInt32(to.Text)>=1&&Convert.ToInt32(to.Text)<=PageCount)
    {
    CurrentPage=Convert.ToInt32(to.Text)-1;

    }
    else
    {
    CurrentPage=PageCount-1;
    }
    ListBind();



    }
    }
      

  11.   

    <%@ Page language="c#" Codebehind="priceshow.aspx.cs" AutoEventWireup="false" Inherits="wshd.pricenews.priceshow" %>
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
    <HTML>
    <HEAD>
    <title>priceshow</title>
    <meta content="Microsoft Visual Studio 7.0" name="GENERATOR">
    <meta content="C#" name="CODE_LANGUAGE">
    <meta content="JavaScript" name="vs_defaultClientScript">
    <meta content="http://schemas.microsoft.com/intellisense/ie5" name="vs_targetSchema">
    </HEAD>
    <body MS_POSITIONING="GridLayout">
    <form id="priceshow" method="post" runat="server">
    <table cellSpacing="0" cellPadding="0" align="center" border="0">
    <tr>
    <td><asp:datalist id="DataList1" runat="server">
    <ItemTemplate>
    <table border="1" width="760" height="84" cellpadding="0" cellspacing="0" align="center">
    <tr>
    <td width="76%" height="18"><%#DataBinder.Eval(Container.DataItem,"title")%></td>
    <td width="24%" height="18"><%#DataBinder.Eval(Container.DataItem,"date_time")%></td>
    </tr>
    <tr>
    <td width="100%" height="16" colspan="2">
    <table border="1" width="100%" height="108" cellpadding="0" cellspacing="0">
    <tr>
    <td width="94%" height="102"><%#DataBinder.Eval(Container.DataItem,"context")%></td>
    </tr>
    </table>
    </td>
    </tr>
    <tr>
    <td colspan="2">
    <hr color="#333399">
    </td>
    </tr>
    </table>
    </ItemTemplate>
    </asp:datalist></td>
    </tr>
    </table>
    </TD></TR></TABLE>
    <table cellSpacing="0" cellPadding="0" width="760" align="center" border="1">
    <tr>
    <td><FONT size="2">一共</FONT>
    <asp:label id="lblallcount" ForeColor="#0000ff" Runat="server" Font-size="8pt" font-names="verdana"></asp:label><FONT size="2">条记录</FONT>
    <FONT size="2">共</FONT>
    <asp:label id="lblpagecount" runat="server" ForeColor="#0000ff" Font-Names="Verdana" Font-Size="8pt"></asp:label><FONT size="2">页</FONT>
    <FONT size="2">第</FONT>
    <asp:label id="lblcurrentindex" runat="server" ForeColor="#0000ff" Font-Names="Verdana" Font-Size="8pt"></asp:label><FONT size="2">页</FONT>
    </td>
    <td align="right"><asp:linkbutton id="first" runat="server" font-size="9pt" CommandArgument="first" ForeColor="#3300ff" Font-Names="Verdana"></asp:linkbutton>&nbsp;<asp:linkbutton id="prev" CommandArgument="prev" ForeColor="#3300ff" Font-Names="Verdana" Runat="server" Font-Size="9pt"></asp:linkbutton>&nbsp;<asp:linkbutton id="next" CommandArgument="next" ForeColor="#3300ff" Font-Names="Verdana" Runat="server" Font-Size="9pt"></asp:linkbutton>&nbsp;<asp:linkbutton id="last" CommandArgument="last" ForeColor="#3300ff" Font-Names="Verdana" Runat="server" Font-Size="9pt"></asp:linkbutton><FONT size="2">跳转到第
    <asp:textbox onkeypress="return event.keyCode>=48&amp;&amp;event.keyCode<=57" onpaste="return !clipboardData.getData('text').match(/\D/)" id="to" ondragenter="return false" style="IME-MODE: disabled" runat="server" Width="28px" MaxLength="5"></asp:textbox></FONT>
    <FONT size="2">页</FONT>
    <asp:button id="go" Font-Names="Verdana" Runat="server" Text="GO" BackColor="WhiteSmoke"></asp:button></td>
    </tr>
    </table>
    </form>
    </body>
    </HTML>
      

  12.   

    问题已经得到解决!我在此感谢donglicsdn(囡囡)以及caojingui(快乐的人) 。