我用 Microsoft Visual Studio 2005 做一个网页,网页上面要显示很多的图片。我想要将它分页显示,要如何写代码啊。不用数据库可以做到吗?同时我还要将照片分类,用导航控件“menu”可以做到吗?怎样写代码啊?
     那位大侠指点一下咯,小弟初学.net ,还是自己摸索的。
     给我详细的代码啊
     感激不尽!!!!!!

解决方案 »

  1.   

    分页的,你拿回去,修改成你需要的就OK了!要分类,我不用Menu!<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Products.aspx.cs" MasterPageFile ="~/MasterPage.master" Inherits="pages_Products" %>
    <% @ Import Namespace="System.Data" %>
    <% @ Import Namespace="System.Data.OleDb" %>
    <script Language="C#" Runat="Server">OleDbConnection MyConn;
    int PageSize,RecordCount,PageCount,CurrentPage;
    string sort;
    public void Page_Load(Object src,EventArgs e)

       if (Page.Request["sort"]!=null)
       {
            sort=" where sort='"+Page.Request["sort"]+"' ";   
       }
        else
        {
            sort=" ";
        }
        
     //设定PageSize
     PageSize = 15; //连接语句
     string MyConnString = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source="+Server.MapPath("../app_data/NYdb.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=0;
     string strCount = "select [id], [pro_name],[image] from 产品表"+sort+"order by id desc";
     OleDbCommand MyComm = new OleDbCommand(strCount,MyConn);
     OleDbDataReader dr = MyComm.ExecuteReader();
     //if(dr.Read())
     //{
      //intCount = Int32.Parse(dr["co"].ToString());
     // intCount=intCount+1;
     //}
     //else
     //{
     // intCount = 0;
     //}
     while (dr.Read())
     {
        intCount=intCount+1;
     }
     dr.Close();
     return intCount;
    }ICollection CreateSource()
    { int StartIndex; //设定导入的起终地址
     StartIndex = CurrentPage*PageSize;
     string strSel = "select [id], [pro_name],[image] from 产品表"+sort+"order by id desc";
     DataSet ds = new DataSet(); OleDbDataAdapter MyAdapter = new OleDbDataAdapter(strSel,MyConn);
     MyAdapter.Fill(ds,StartIndex,PageSize,"Score"); return ds.Tables["Score"].DefaultView;
    }
    public void ListBind()
    {
     DataList1.DataSource = CreateSource();
     DataList1.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>
    <asp:Content ID="Content1" runat="server" ContentPlaceHolderID="ContentPlaceHolder1">
        <table style="width: 100%">
            <tr>
                <td style="width: 100%">
                    <table style="width: 100%">
                        <tr>
                            <td style="width: 20%">
                                <a id ="1"></a><strong>产品展示</strong></td>
                            <td style="width: 80%; background-color: #e5f5eb;">
                            </td>
                        </tr>
                    </table>
                    <asp:DataList ID="DataList1" runat="server" RepeatColumns="4" RepeatDirection="Horizontal" CellPadding="1" CellSpacing="5" Width="100%">
                        <ItemTemplate>
                            &nbsp;<table style="width: 150px">
                                <tr>
                                    <td style="width: 150px; text-align: center;">
                                        <a href ='<%# Eval("id", "../pages/product.aspx?id={0}") %>'><asp:Image ID="Image1" runat="server" Height="123px" Width="118px" AlternateText='<%# Eval("pro_name") %>' ImageUrl='<%# Eval("image", @"../images/{0}") %>' /></a></td>
                                </tr>
                                <tr>
                                    <td style="width: 150px; text-align: center;">
                                        <asp:HyperLink ID="HyperLink3" runat="server" NavigateUrl='<%# Eval("id", "~/pages/product.aspx?id={0}") %>'
                                                            Text='<%# Eval("pro_name", "{0}") %>' ToolTip='<%# Eval("pro_name") %>'></asp:HyperLink></td>
                                </tr>
                            </table>
                        </ItemTemplate>
                    </asp:DataList></td>
            </tr>
            <tr>
                <td style="width: 100%; text-align: center;" valign ="top">
                    &nbsp;共有<asp:Label ID="lblRecordCount" runat="server" ForeColor="red"></asp:Label>条记录&nbsp;
                    当前为<asp:Label ID="lblCurrentPage" runat="server" ForeColor="red"></asp:Label>/<asp:Label
                        ID="lblPageCount" runat="server" ForeColor="red"></asp:Label>页
                    <asp:LinkButton ID="shouye" runat="server" CommandName="shou" OnCommand="Page_OnClick"
                        Text="首页"></asp:LinkButton>
                    <asp:LinkButton ID="lbnPrevPage" runat="server" CommandName="prev" OnCommand="Page_OnClick"
                        Text="上一页"></asp:LinkButton>
                    <asp:LinkButton ID="lbnNextPage" runat="server" CommandName="next" OnCommand="Page_OnClick"
                        Text="下一页"></asp:LinkButton>
                    <asp:LinkButton ID="moye" runat="server" CommandName="mo" OnCommand="Page_OnClick"
                        Text="末页"></asp:LinkButton></td>
            </tr>
        </table>
    </asp:Content>
      

  2.   

    可以在客户端进行分页,用JS处理:将所有图片放在一个固定高度的DIV中,计算实际高度除以滚动条高度得到页数,每一页一个可以点击的标记,点击后是滚动条的位置,就能得到分页效果
      

  3.   

    直接用datalist或跟它相式的那几个控件就可以做了,
      

  4.   

    gridview控件也可以实现,它有分页的功能..方法很多的
      

  5.   

    js处理最好了 只要更换img 的src就可以了
      

  6.   

    分页可以采用 AspNetPager控件,很好用,
    参考这里:
    http://www.cnblogs.com/pochonlee/archive/2008/05/17/1201374.html分类是在数据库设置好的。Menu只是菜单控件。