上传图片按钮未触发

时间:2014-11-19 02:18:49

标签: asp.net sql-server vb.net stored-procedures

我有一个像这样的上传图片按钮

ASP.NET

     <asp:FileUpload ID="fulProduct" runat="server" CssClass="one-third-input"  onchange="Validate(this.id);" />
      <asp:Button ID="BtnUploadImg" runat="server" CssClass="button round gray image-left ic-save" OnClick="BtnUploadImg_Click" Text="Submit Image"/>

并编写代码以将文件(图像文件)上传到ImageNameImageEncrypt VB.NET

 Protected Sub saveimg(ByVal DocumentStatusID As Integer)
    Dim Conn As New SqlConnection(strConn)
    Dim cmd As New SqlCommand

    cmd.Connection = Conn
    cmd.CommandType = CommandType.StoredProcedure
    cmd.CommandText = "spProducts"
    cmd.Parameters.AddWithValue("@Type", "UPDATE")
    Try
        If ((Not txtProducerID.Text.Trim = "") And (Not lblGetProductID.Text = "")) Then
            If fulProduct.HasFile = True Then
                Dim fileSize As String = fulProduct.PostedFile.ContentLength

                If (fileSize < 1024000) Then ' 1MB approx (actually less though)
                    'Upload new file
                    Dim EncryptName As String
                    pnlUpload.Visible = False
                    pnlSave.Visible = True
                    Dim fileName As String = Path.GetFileNameWithoutExtension(fulProduct.PostedFile.FileName)
                    Dim fileExtension As String = Path.GetExtension(fulProduct.PostedFile.FileName.ToLower)

                    EncryptName = Encrypt.MD5(fileName & Now) & fileExtension
                    fulProduct.SaveAs(Server.MapPath("~/FileStore/" & EncryptName))
                    lblPicture.Text = "<img src='../FileStore/" & EncryptName & "' height='300' />"

                    cmd.Parameters.AddWithValue("@ImageName", fileName & fileExtension)
                    cmd.Parameters.AddWithValue("@ImageEncrypt", EncryptName)

                    hplPictureView.NavigateUrl = "~/FileStore/" & EncryptName
                    hplPictureView.Target = "_blank"
                    chkPictureDelete.Checked = False
                End If

            ElseIf chkPictureDelete.Checked = True Then
                'Delete exist file
                Dim FileToDelete As String

                FileToDelete = Server.MapPath(hplPictureView.NavigateUrl)
                If File.Exists(FileToDelete) = True Then
                    File.Copy(FileToDelete, Server.MapPath(hplPictureView.NavigateUrl.Replace("~/FileStore/", "~/FileStore/Deleted/")))
                    File.Delete(FileToDelete)
                    cmd.Parameters.AddWithValue("@ImageName", "")
                    cmd.Parameters.AddWithValue("@ImageEncrypt", "")
                End If
                pnlUpload.Visible = True
                pnlSave.Visible = False
            End If

            Conn.Open()
            Dim ResponseText As Integer = DirectCast(cmd.ExecuteScalar, Int32).ToString
            If (ResponseText = 1) Then
                lblResponseResult.Text = "<img src='../images/icons/message-boxes/confirmation.png' /> บันทึกลงฐานข้อมูลเรียบร้อยแล้ว"
                If (DocumentStatusID = 2) Then 'Goto next step
                    AlertRedirect("redirect", pnlHint.ClientID, 3, "Products.aspx")
                Else
                    AlertBox("alert", pnlHint.ClientID, 2)
                End If
            End If
        Else
            Dim sb As New System.Text.StringBuilder()
            sb.Append("<script type = 'text/javascript'>")
            sb.Append("window.onload=function(){")
            sb.Append("javascript:history.back();return false;")
            sb.Append("};")
            sb.Append("</script>")
            ClientScript.RegisterClientScriptBlock(Me.GetType(), "back", sb.ToString())
        End If

    Catch ex As Exception
        Response.Write("Error Save: " & ex.Message)
    Finally
        Conn.Close()
    End Try
End Sub

并创建一个事件

    Protected Sub BtnUploadImg_Click(sender As Object, e As EventArgs)
    SaveProduct(1)
End Sub

但所有这些按钮都没有触发它只是重置文件上载字段

1 个答案:

答案 0 :(得分:0)

添加回发后触发器并尝试..

<Triggers>  
     <asp:PostBackTrigger ControlID = "BtnUploadImg" />
</Triggers>