Create online album with ASP.NET

xiaoxiao2021-03-06  42

In the current digital age, we will often take some photos for remembrance, and as digital photos increase, it is often necessary to manage these photos to better check the age. Now there are a lot of electronic photo albums on the Internet, you can implement these features well, can we create your own albums yourself? Of course, in this article, we will use ASP.NET to create a simple online photo album to collect our photos.

First, look at this album. In this album, we must first put the pre-photographic photos in a directory, then view the link to "Previous, Next" link.

Let's first describe how to get pictures in the folder. We can implement the DirectoryInfo class in the System.io namespace. Transfer the path in the folder as a parameter into the constructor of the class and declares an instance of a DirectoryInfo class. There is a getFiles () method in the DirectoryInfo class, which returns fileInfo object array, and each fileInfo instance will contain specific information on the file under the specified path. The following code snippet shows the process:

Sub Page_Load (Sender As Object, E AS Eventargs) 'get list of images DIRINFO AS New DirectoryInfo (Server.MAppath (")) DIM images () as fileinfo = filterforimages (Dirinfo.getFiles ()) ...

End Sub

Among them, get the path to the current directory with server.mappath, and Dirinfo.getFiles () will return all files in this directory. Because of our photo album, just see image files such as JPG, BMP, GIF, so we can implement these types of files, which is implemented by a custom process FilterforImages, which will be implemented. Returns files for image types in the specified folder. code show as below:

Function FilterforImages (images () as fileinfo) as fileinfo () DIM newImages as new arraylist (images.length)

Dim i as integer for i = 0 to images.length - 1 if path.Getextension (iMages (i) .name) = ".jpg" Oralse _ path.getExtension (images (i) .name) = ".jpeg" ORSE _ Path.Getextension (images (i) .name) = ".png" ORELSE _ path.Getextension (images (i) .name) = ".gif" Then newImages.Add (i)) endiffs.

Return CType (NewImages.Toarray (Gettype (FileInfo), FileInfo ()) End Function

The process is traversed for traversing the fileinfo parameter array, and the suffix name of the file in the folder, if it belongs to the image file, add it to the newImages array and returned in the form of arraylist.

Next, let's see how each picture is displayed, and it is displayed in "Previous, Next". In order to know that the presently browsing is the first picture, it can be implemented by the method of transmitting parameters. To add an image control and text box in the form, the program code is as follows: SUB Page_Load (Sender As Object, E AS Eventargs) ...

'DIM IMGINDEX AS INTEGER = 0 if NOT Request.QueryString ("N") is nothing andalso _ isnuMeric ("n")) The imgindex = cint (Request.QueryString ("n")) End if currentimgtitle. Text = "you are viewing:" & _ path.getFileNamewithOUTEXTENSITION (IMGINDEX) .NAME) & _ "(" & IMGINDEX 1 & "OF IMAGES.LENGTH &") "currentimg.imageurl = path.getFileName Images (imgindex) .name ... END SUB

HTML part of the code


In the above code, use the variable imgindex to indicate the presently browsing is the first picture, just at the beginning n = 0, get the first variable in the Image array, that is, the first picture, then read each time This variable can be known that the presently browsing is the first few pictures.

In order to achieve the "next, previous" function, add two HyperLink link controls to the form, and add the following code

SUB Page_Load (Sender As Object, E AS Eventargs) ...

IF imgindex> 0 Then lnkprev.navigateURL = "default.aspx? N =" & imgindex - 1END IF

IF imgindex

HTML part of the code

|

The above code is relatively easy to understand, when the next one, the previous link, the value of the parameter n is 1, or minus 1.

Finally, in order to achieve a relatively intuitive effect, we placed a DataList control, where all files under the image folder are displayed. Whenever you browse a new picture, add the name of the picture currently browsed in the form of a link. Bright display, the code is as follows: SUB Page_Load (Sender As Object, E AS Eventargs) ...

Dlindex.datasource = images Dlindex.database DLINDEX.DATABIND () End Sub

Sub dlIndex_ItemDataBound (sender as Object, e as DataListItemEventArgs) If e.Item.ItemType = ListItemType.Item OrElse _ e.Item.ItemType = ListItemType.AlternatingItem then 'Get the Hyperlink Dim hl as HyperLink = CType (e.Item.FindControl ( "lnkpic"), HyperLink

'Set the text and navigation proties hl.text = path.getFileNamewithoutextension (_ DatabaseM.Eval ("name"). Tostring ()) &_ "(" & _ INT (DataBinder.eval (e. Item.DataItem, "Length") / 1000) & _ "KB)" hl.navigateurl = "default.aspx? N =" & E.Item.itemindex End IFEND SUB

HTML part of the code

  • In the above code, in the DataList's OnItemDatabase, first determine if the currently triggered item is a list item ListItemType or alternate alternatingItem. If so, dynamically generate a link HL, set the value of HL to the current view image. The file name, and indicate the size of the file, set the address of its link as the address of the current browsing image, so that the user can click the picture to be browsed, different from the previous, the next link to fulfill.

    Finally, one example is given (http://aspnet.4guysfromrolla.com/london/) and all code:

    <% @ Import namespace = "system.io"%>