关闭

分页的例子(吓搞了一个)

标签: hyperlinkdatasetservertableaspsession
647人阅读 评论(0) 收藏 举报
分类:

后台:

protected void Page_Load(object sender, System.EventArgs e)
  {
   if(!IsPostBack)
   {
    SqlConnection conn=db.cn();
    conn.Open();
    SqlCommand cmd=new SqlCommand();
    cmd.Connection=conn;
                cmd.CommandText = "SELECT COUNT(*) FROM gg WHERE (title LIKE '%是%')";
    ViewState["count"]=cmd.ExecuteScalar();
    
    SqlDataAdapter da=new SqlDataAdapter();
    DataSet ds=new DataSet();    

    int page=10;
    page=page*(Convert.ToInt32(Request.Params["page"])-1);
    if(page>0)
    {
                    cmd.CommandText = "SELECT TOP 10 * FROM gg WHERE (id NOT IN (SELECT TOP " + page + " id FROM gg WHERE (title LIKE '%是%'))) and (title LIKE '%是%')";
    }
    else
    {
     cmd.CommandText="SELECT TOP 11 * FROM gg";
    }
    da.SelectCommand=cmd;
    da.Fill(ds);
    PagedDataSource objPage=new PagedDataSource();

    objPage.DataSource=ds.Tables[0].DefaultView;

    objPage.AllowPaging=true;

    objPage.PageSize=10;

    int CurPage;
    if(Request.QueryString["Page"]!=null)
     CurPage=Convert.ToInt32(Request.QueryString["Page"]);
    else CurPage=1;
    objPage.CurrentPageIndex=CurPage-1;
    if(objPage.CurrentPageIndex%20==0)
    {
     Session["begin"]=objPage.CurrentPageIndex+1;
    }

    Label1.Text="当前页:第"+CurPage.ToString()+"页";
    if(!objPage.IsFirstPage)
     HyperLink1.NavigateUrl=Request.CurrentExecutionFilePath+ "?Page=" +Convert.ToString(CurPage-1);
    if(!objPage.IsLastPage)
     HyperLink2.NavigateUrl=Request.CurrentExecutionFilePath+ "?Page=" +Convert.ToString(CurPage+1);
    DataList1.DataSource=ds;
    DataList1.DataBind();
    ds.Clear();
    
    conn.Close();
   }
  }

  public void GetString()
  {
            int i = Convert.ToInt32(Session["begin"]);
        if (Convert.ToInt32(Request.Params["page"]) % 20 == 0 && Request.Params["page"] != null)
        {
            i = i - 20;
        }
        if ( Convert.ToInt32(Request.Params["page"])>i + 20)
        {
            i = i+20;
        }
   for(;i <= (Convert.ToInt32(ViewState["count"])+10)/10;i++)
   {
                if (i%20 == 1)
                {
                    if (i == 1)
                    { }
                    else
                    {
                        Response.Write("</TD>&nbsp;<a href='" + Request.CurrentExecutionFilePath + "?page=" + (i - 20) + "' alt=上20页>上20页</a>&nbsp;</TD>");
                    }
                } 
    Response.Write("</TD>&nbsp;<a href='"+Request.CurrentExecutionFilePath+"?page="+i+"'>"+i+"</a>&nbsp;</TD>");
    if(i%20==0)
    {
     i++;
                    Response.Write("</TD>&nbsp;<a href='" + Request.CurrentExecutionFilePath + "?page=" + i + "' alt=下20页>下20页</a>&nbsp;</TD>");
     break;
    }
   }  
  }

前台:

<form id="Form1" method="post" runat="server">
   <TABLE id="Table1" style="Z-INDEX: 101; LEFT: 40px; WIDTH: 704px; POSITION: absolute; TOP: 24px; HEIGHT: 163px"
    cellSpacing="1" cellPadding="1" width="704" border="1">
    <TR>
     <TD style="WIDTH: 63px"><FONT face="宋体"></FONT></TD>
     <TD style="WIDTH: 516px"><FONT face="宋体"><asp:datalist id="DataList1" runat="server">
        <ItemTemplate>
         <%#DataBinder.Eval(Container.DataItem,"title").ToString() %>
        </ItemTemplate>
        <SeparatorTemplate>
         ......................
        </SeparatorTemplate>
        <AlternatingItemTemplate>
         <%#DataBinder.Eval(Container.DataItem,"title").ToString() %>
        </AlternatingItemTemplate>
       </asp:datalist></FONT></TD>
     <TD><FONT face="宋体"></FONT></TD>
    </TR>
    <TR>
     <TD style="WIDTH: 63px"><FONT face="宋体"><asp:hyperlink id="HyperLink1" runat="server">HyperLink</asp:hyperlink></FONT></TD>
     <TD style="WIDTH: 516px">
      <table>
       <%GetString();%>
      </table>
     </TD>
     <TD><FONT face="宋体"><asp:hyperlink id="HyperLink2" runat="server">HyperLink</asp:hyperlink><asp:label id="Label1" runat="server">当前第XXX页</asp:label></FONT></TD>
    </TR>
   </TABLE>
  </form>

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:19737次
    • 积分:384
    • 等级:
    • 排名:千里之外
    • 原创:19篇
    • 转载:3篇
    • 译文:0篇
    • 评论:1条
    最新评论
    asp.net论坛