none
MS Knowledge Base: Bagaimana menggunakan DataList control untuk membuat Image Gallery? (Part 2) RRS feed

  • Diskusi Umum

  •  

    6. Gunakan kode berikut untuk mengimport System.Data dan System.IO namespaces ke

        halaman:

        	
    		using System.Data;
    		using System.IO;
       

    7. Sisipkan kode berikut untuk membuat 2(dua) properti yang baru, Page_Index dan Page_Count:

    //property for current page index

    public int Page_Index

    {

        get { return (int)ViewState["_Page_Index"]; }

        set { ViewState["_Page_Index"] = value; }

    }

    //property for total page count

    public int Page_Count

    {

        get { return (int)ViewState["_Page_Count"]; }

    set { ViewState["_Page_Count"] = value; }

    }

    8. Sisipkan kode berikut untuk mendapatkan jumlah gambar:

    //return total number of images

    protected int ImageCount()

    {

        DirectoryInfo di = new DirectoryInfo(Server.MapPath("/Image/"));

        FileInfo[] fi = di.GetFiles();

        return fi.GetLength(0);

    }

    9. Sisipkan kode berikut untuk menggabungkan kontrol DataList:

    //return the data source for DataList

            protected DataTable BindGrid()

            {

                //get all image paths            

                DirectoryInfo di = new DirectoryInfo(Server.MapPath("/Image/"));

                FileInfo[] fi = di.GetFiles();

     

                //save all paths to the DataTable as the data source

                DataTable dt = new DataTable();

                DataColumn dc = new DataColumn("Url", typeof(System.String));

                dt.Columns.Add(dc);

               int lastindex = 0;

                if (Page_Count == 0 || Page_Index == Page_Count - 1)

                {

                   lastindex = ImageCount();

                }

                else

                {

                    lastindex = Page_Index * PageSize + 5;

                }

                for (int i = Page_Index * PageSize; i < lastindex; i++)

                {

                    DataRow dro = dt.NewRow();

                    dro[0] = fi[i].Name;

                    dt.Rows.Add(dro);

                }

                return dt;

            }

    Berlanjut ke MS Knowledge Base: Bagaimana menggunakan DataList control untuk membuat Image Gallery? (Part 3)


    Agnes Sannie [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Rabu, 01 Juni 2011 07.11
    Moderator