我在asp.net(VS2012)中写了这个事件。我希望用户上传图像和说明,上传后我想在页面的网格视图中显示图像名称和描述,并允许上传下一张图像。我不确定为什么事件被调用两次,第二次我在“if strfile.hasfile ...”中得到“Object reference not set to ....”错误。希望我能正确解释。
Protected Sub btnUpload_Click(sender As Object, e As EventArgs) Handles btnUpload.Click
Dim strfilename, strfilepath As String
strfilename = Nothing
strfilepath = lblInciID.Text.Trim
Try
' Dim CurrentPath As String = Server.MapPath("C:\DSimages\")
If strFile.HasFile Then
Try
strFile.SaveAs("C:\DSimages\" & _
strfilepath + strFile.FileName)
'Save the name in the table
strfilename = strFile.PostedFile.FileName
strfilename = strfilepath + strfilename
If strFile.HasFile Then strFile = Nothing
Catch ex As Exception
MsgBox("ERROR: " & ex.Message.ToString())
End Try
End If
ImageDS.InsertParameters("ImagePath").DefaultValue = strfilename.Trim
ImageDS.InsertParameters("ImageDescription").DefaultValue = txtPicDesc.Text.Trim()
ImageDS.InsertParameters("InciID").DefaultValue = lblInciID.Text.Trim()
ImageDS.InsertParameters("Category").DefaultValue = lblCategory.Text.Trim()
ImageDS.Insert()
'MsgBox("Picure was uploaded successfully! Upload another picture or click on 'Cancel'")
txtPicDesc.Text = ""
Catch ex As Exception
MsgBox(ex.Message)
End Try
GridView_Image.Visible = True
End Sub
aspx页面:
<div class="clearclass padtop">
<div class="div2">
<h4 style="width:23.2em">Browse file</h4>
<asp:FileUpload runat="server" ID="strFile" width="25em" Height="30px"/><br />
<asp:Label runat="server" ID="imgtoupload"></asp:Label>
<asp:RequiredFieldValidator ID="RequiredFieldValidatorFile" runat="server" ErrorMessage="File" ControlToValidate="strFile"
CssClass="fontred style_bold" Display="Static">File</asp:RequiredFieldValidator>
</div>
<div class="div2">
<h4 style="width:24.6em">Add Description</h4>
<asp:TextBox runat="server" ID="txtPicDesc" Width="30.4em" Height="5em" TextMode="MultiLine"></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidatorDesc" runat="server" ControlToValidate="txtPicDesc" CssClass="fontred style_bold"
ErrorMessage="Description">Description</asp:RequiredFieldValidator>
</div>
</div>
<asp:GridView ID="GridView_Image" runat="server" DataSourceID="ImageDS" AutoGenerateColumns="False" HorizontalAlign="Center" Visible="false">
<Columns>
<asp:BoundField DataField="InciID" HeaderText="InciID" SortExpression="InciID" />
<asp:BoundField DataField="Imagepath" HeaderText="Imagepath" SortExpression="Imagepath" />
<asp:BoundField DataField="ImageDescription" HeaderText="ImageDescription" SortExpression="ImageDescription" />
</Columns>
<RowStyle BackColor="#EFF3FB" Height="2em" />
</asp:GridView>