ASP. NET to add, update, delete, and display images with DataList

Source: Internet
Author: User

In a recent project (VB. NET), one of the functions is to allow users to manage the marquee images in the background. You can upload, update, and delete images. Insus. NET uses asp: FileUpload of asp.net to upload and use asp: DataList for display. Uploading, displaying, and deleting images may not be difficult for everyone. This blog post is a function for sharing and updating images. Select an image of DataList, select an image file for the update, and then update the file. First look at the demo Animation:

 


Write an event for DataList: OnItemCreated = "DataListGallery_ItemCreated"
Write this event in vb.net:
View Code
Protected Sub DataListGallery_ItemCreated (sender As Object, e As DataListItemEventArgs)
'Determine whether the button for editing the ammonium is in the AlternatingItem or Item of DataList.
If e. Item. ItemType = ListItemType. AlternatingItem OrElse e. Item. ItemType = ListItemType. Item Then
'Edit whether the object of the ammonium button is empty
If e. Item. FindControl ("ImageButtonEdit") IsNot Nothing Then
'If not empty, convert the object to the ImageButton control.
Dim ImageButtonEdit As ImageButton = DirectCast (e. Item. FindControl ("ImageButtonEdit"), ImageButton)
'Writes a Click event for ImageButton.
AddHandler ImageButtonEdit. Click, AddressOf ImageButtonEdit_Click
End If
End If
End Sub

 
When you click, you need to change the background Color of the current column. Another key point is to get the primary key of the image and save it with session.
View Code
Private Sub ImageButtonEdit_Click (sender As Object, e As ImageClickEventArgs)
'Convert the sender object to the ImageButton control.
Dim imageButton As ImageButton = DirectCast (sender, ImageButton)
'Convert the Parent control of the ImageButton control into DataListItem
Dim dataListItem As DataListItem = DirectCast (imageButton. Parent, DataListItem)
'Change the Bar background Color
DataListItem. BackColor = Drawing. Color. Red
'Get the primary key and save it to the Session.
Session ("SlideMarqueeGalleryId") = Me. DataListGallery. DataKeys (dataListItem. ItemIndex). ToString ()
End Sub

 
Update event:
View Code
Protected Sub btnUpdate_Click (sender As Object, e As EventArgs)
'If the user does not select the image to be edited, the user is prompted to select the image. The Session is empty, that is, the user does not select an image.
If String. IsNullOrEmpty (DirectCast (Session ("SlideMarqueeGalleryId"), String) Then
ObjInsusJsUtility. JsAlert ("select the image to be edited. ")
Return
End If

'Determine whether an image is selected
If Not Me. FileUpload2.HasFile Then
ObjInsusJsUtility. JsAlert ("Select Upload File. ")
Return
End If

'Use the Path static method GetExtension () in the IO namespace to get the image extension, that is, to get the image format
Dim extension As String = System. IO. Path. GetExtension (Me. FileUpload2.PostedFile. FileName)

'Create a file name, including the path
Dim newFileName As String = Guid. NewGuid (). ToString () & extension
'Create a file and store it in the temporary directory of the project.
Dim file As String = Server. MapPath (InsusBase. TemporaryDirectory () & newFileName)
'Save the file
Me. FileUpload2.SaveAs (file)

'Determine whether it is an image, this method reference: http://www.bkjia.com/kf/201203/122433.html
If Not IsImage (file) Then
ObjInsusJsUtility. JsAlert ("select the image format file and upload it again. ")
Return
End If

'Convert the file into a stream.
Dim stm As Stream = Me. FileUpload2.PostedFile. InputStream
Try
ObjSlideMarqueeGallery. Update (extension, stm, DirectCast (Session ("SlideMarqueeGalleryId"), String ))
Data_Binding ()
ObjInsusJsUtility. JsAlert ("image uploaded successfully. ")

Catch ex As Exception
ObjInsusJsUtility. JsAlert (ex. Message)
End Try
'After the upload is successful, you need to clear the Session
Session ("SlideMarqueeGalleryId") = Nothing
End Sub

 

From Insus. NET

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.