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