在asp.net上传照片

时间:2010-10-09 01:02:40

标签: asp.net vb.net

您好我有以下代码上传我的应用程序的图片。我想在此代码中添加一行或更多行,以显示成功消息,如“图片已成功上传”

我知道我需要添加一个标签控件来显示消息,但我不知道如何去做。这是代码:

Imports System.Data
Imports System.IO
Imports System.Data.SqlClient

Partial Class PhotoAdmin_Default
    Inherits System.Web.UI.Page


    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        UserIdValue.Text = Membership.GetUser().ProviderUserKey.ToString()
        cannotUploadImageMessage.Visible = False

    End Sub


    Protected Sub dvPictureInsert_ItemInserted(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DetailsViewInsertedEventArgs) Handles dvPictureInsert.ItemInserted
        'If the record was successfully inserted, save the picture
        If e.AffectedRows > 0 Then
            'Determine the maximum pictureID for this user
            Dim results As DataView =
                CType(maxPictureIDDataSource.Select(DataSourceSelectArguments.Empty), 
                    DataView)

            Dim pictureIDJustAdded As Integer = CType(results(0)(0), Integer)

            'Reference the FileUpload control
            Dim imageUpload As FileUpload =
                CType(dvPictureInsert.FindControl("imageUpload"), FileUpload)

            If imageUpload.HasFile Then
                Dim baseDirectory As String = Server.MapPath("~/UploadedImages/")

                imageUpload.SaveAs(baseDirectory & pictureIDJustAdded & ".jpg")
            End If

        End If
    End Sub



    Protected Sub dvPictureInsert_ItemInserting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DetailsViewInsertEventArgs) Handles dvPictureInsert.ItemInserting
        Dim cancelInsert As Boolean = False


        Dim imageUpload As FileUpload =
            CType(dvPictureInsert.FindControl("imageUpload"), FileUpload)

        If Not imageUpload.HasFile Then
            cancelInsert = True
        Else
            If Not imageUpload.FileName.ToUpper().EndsWith(".JPG") Then
                cancelInsert = True 'Invalid image file!

            End If
        End If

        If cancelInsert Then
            e.Cancel = True
            cannotUploadImageMessage.Visible = True
        End If

        'Set the UserId value to the currently logged on user's ID
        e.Values("UserId") = Membership.GetUser().ProviderUserKey

        'Set the UploadedOn value to the current date/time
        e.Values("UploadedOn") = DateTime.Now
    End Sub

    Protected Sub gvPictures_RowDeleted(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewDeletedEventArgs) Handles gvPictures.RowDeleted
        Dim baseDirectory As String = Server.MapPath("~/UploadedImages/")
        Dim fileName As String = baseDirectory &
            e.Keys("PictureID") & ".jpg"
        File.Delete(fileName)
    End Sub

1 个答案:

答案 0 :(得分:0)

感谢上帝回答了这个论坛的问题。这真的很有帮助,单词无法表达它。 答案来自:

您必须在“dvPictureInsert_ItemInserted”事件中显示消息。有关更多参考,请参阅此链接,在此您可以看到示例部分,您可以获得您必须做的事情

http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.formview.iteminserted%28VS.80%29.aspx