我有一个gridview,每行都有一个FileUpload。当您单击文件上载时,它会选择该文件,然后单击上载在gridview行中的按钮上。
我想取消点击上传按钮,只需在浏览后自动上传文件。
我该怎么做?
这是我目前运行的gridview和代码:
<asp:GridView ID="gvParts" runat="server" class="table table-bordered table-highlight" AutoGenerateColumns="False" DataSourceID="SqlDataSource2">
<Columns>
<asp:TemplateField HeaderText="Action">
<ItemTemplate>
<center><asp:ImageButton ID="ImageButton1" runat="server" ImageUrl="~/img/imageicon.png" CommandName="View" Visible="False" /></center>
<asp:FileUpload ID="FileUpload1" runat="server" Width="104px" />
<asp:Button ID="Button1" runat="server" CommandArgument='<%# Container.DataItemIndex %>'
Text="Upload" OnClick="Button1_Click" />
</ItemTemplate>
<ItemStyle Width="100px" />
</asp:TemplateField>
<asp:BoundField DataField="PART_ID" HeaderText="Part ID" SortExpression="PART_ID" />
<asp:BoundField HeaderText="Has Image" />
<asp:BoundField DataField="PART_NUMBER" HeaderText="Part Number" SortExpression="PART_NUMBER" />
<asp:BoundField DataField="DESCRIPTION" HeaderText="Description" SortExpression="DESCRIPTION" />
<asp:BoundField DataField="DESCRIPTION2" HeaderText="Description 2" SortExpression="DESCRIPTION2" />
</Columns>
</asp:GridView>
这是我的代码,点击上传按钮后执行(我试图消除,所以我只需选择文件并自动上传)
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs)
For Each row As GridViewRow In gvParts.Rows
Dim file As FileUpload = CType(row.FindControl("FileUpload1"), FileUpload)
If (Not (file) Is Nothing) Then
If file.HasFile Then
' Read the file and convert it to Byte Array
Dim filePath As String = file.PostedFile.FileName
Dim filename As String = Path.GetFileName(filePath)
Dim ext As String = Path.GetExtension(filename)
Dim contenttype As String = String.Empty
'Set the contenttype based on File Extension
Select Case ext
Case ".jpg"
contenttype = "image/jpg"
Exit Select
Case ".png"
contenttype = "image/png"
Exit Select
Case ".gif"
contenttype = "image/gif"
Exit Select
End Select
If contenttype <> String.Empty Then
Dim fs As Stream = file.PostedFile.InputStream
Dim br As New BinaryReader(fs)
Dim bytes As Byte() = br.ReadBytes(fs.Length)
'insert the file into database
Dim strQuery As String = "insert into tbl_TMGPartImages (PartID, ImageName, ContentType, Data) values (@PartID, @ImageName, @ContentType, @Data)"
Dim cmd As New SqlCommand(strQuery)
cmd.Parameters.AddWithValue("@PartID", row.Cells(1).Text)
cmd.Parameters.Add("@ImageName", SqlDbType.VarChar).Value = filename
cmd.Parameters.Add("@ContentType", SqlDbType.VarChar).Value _
= contenttype
cmd.Parameters.Add("@Data", SqlDbType.Binary).Value = bytes
Try
Dim con8 As SqlClient.SqlConnection
Dim cmd8 As SqlClient.SqlCommand
con8 = New SqlClient.SqlConnection(System.Configuration.ConfigurationManager.AppSettings("MainConnectionString").ToString)
con8.Open()
cmd8 = New SqlClient.SqlCommand
cmd8 = New SqlCommand("Delete from [TMGGoods].[dbo].[tbl_TMGPartImages] where PartID = @PartID", con8)
cmd8.Parameters.AddWithValue("@PartID", row.Cells(1).Text)
cmd8.ExecuteNonQuery()
con8.Close()
Catch ex As Exception
End Try
InsertUpdateData(cmd)
Else
End If
End If
End If
Next
End Sub