我的asp.net 4.5 VB应用程序页面上有一个FormView。我在FormView的EditItemTemplate
和InsertItemTemplate
中有一个FileUpload。 InsertItemTemplate
按预期正常工作。我想要我的FormView:
的FormView:
<asp:FormView ID="FormView1" runat="server" AllowPaging="True" DataKeyNames="KurumId" DataSourceID="KurumlarDS" Width="500px">
<EditItemTemplate>
KurumId:
<asp:Label ID="KurumIdLabel1" runat="server" Text='<%# Eval("KurumId") %>' />
<br />
KurumAdi:
<asp:TextBox ID="KurumAdiTextBox" CssClass="form-control" runat="server" Text='<%# Bind("KurumAdi") %>' />
<br />
KurumLogoPath:
<%--<img runat="server" ID="imgKurumLogo" src='<%# Eval("KurumLogoPath", "../images/kurum/{0}?w=80")%>' alt="Kurum Logosu" />
<br />--%>
<asp:FileUpload ID="KurumLogoPathFUU" runat="server" />
<br />
KurumTelefon:
<asp:TextBox ID="KurumTelefonTextBox" CssClass="form-control" runat="server" Text='<%# Bind("KurumTelefon") %>' />
<br />
KurumBelgeGecer:
<asp:TextBox ID="KurumBelgeGecerTextBox" CssClass="form-control" runat="server" Text='<%# Bind("KurumBelgeGecer") %>' />
<br />
KurumWebAdresi:
<asp:TextBox ID="KurumWebAdresiTextBox" CssClass="form-control" runat="server" Text='<%# Bind("KurumWebAdresi") %>' />
<br />
KurumEMail:
<asp:TextBox ID="KurumEMailTextBox" CssClass="form-control" runat="server" Text='<%# Bind("KurumEMail") %>' />
<br />
KurumTarihce:
<asp:TextBox ID="KurumTarihceTextBox" CssClass="form-control" runat="server" Text='<%# Bind("KurumTarihce") %>' />
<br />
KurumRengi:
<asp:TextBox ID="KurumRengiTextBox" CssClass="form-control" runat="server" Text='<%# Bind("KurumRengi") %>' />
<br />
KurumAdres:
<asp:TextBox ID="KurumAdresTextBox" CssClass="form-control" runat="server" Text='<%# Bind("KurumAdres") %>' TextMode="MultiLine" />
<br />
KurumSortExpression:
<asp:TextBox ID="KurumSortExpressionTextBox" CssClass="form-control" runat="server" Text='<%# Bind("KurumSortExpression") %>' />
<br />
KurumLat:
<asp:TextBox ID="KurumLatTextBox" CssClass="form-control" runat="server" Text='<%# Bind("KurumLat") %>' />
<br />
KurumLon:
<asp:TextBox ID="KurumLonTextBox" CssClass="form-control" runat="server" Text='<%# Bind("KurumLon") %>' />
<br />
<asp:LinkButton ID="UpdateButton" runat="server" CausesValidation="True" CommandName="Update" Text="Güncelle" />
<asp:LinkButton ID="UpdateCancelButton" runat="server" CausesValidation="False" CommandName="Cancel" Text="Vazgeç" />
</EditItemTemplate>
<InsertItemTemplate>
</InsertItemTemplate>
<ItemTemplate>
</ItemTemplate>
<PagerSettings Mode="NumericFirstLast" Position="TopAndBottom" />
相关CodeBehind:
Private Sub FormView1_ItemUpdating(sender As Object, e As FormViewUpdateEventArgs) Handles FormView1.ItemUpdating
Dim fu As FileUpload = CType(FormView1.FindControl("KurumLogoPathFUU"), FileUpload)
Dim MyPath As String = Server.MapPath("~/images/kurum/")
Dim fName As String = fu.FileName
Dim FullPathedFile As String = MyPath & fName
Try
If fu.HasFile = True Then
Dim oldName As String = e.OldValues("KurumLogoPath")
Dim newName As String = e.NewValues("KurumLogoPath")
If File.Exists(MyPath & oldName) Then
File.Delete(MyPath & oldName)
End If
e.NewValues("KurumLogoPath") = fName
fu.SaveAs(FullPathedFile)
Else
e.NewValues("KurumLogoPath") = e.OldValues("KurumLogoPath")
End If
Catch ex As Exception
Response.Write("Cannot Update!" & ex.Message())
e.Cancel = True
End Try
End Sub
我可以看到MyPath
但不是e.NewValues("KurumLogoPath")
或e.OldValues("KurumLogoPath")
我也尝试e.NewValues.Item("KurumLogoPath")
和e.NewValues.Item("KurumLogoPath")
但没有成功。所以我认为所有e.OldValues
和e.NewValues
都为空。因此,file.delete
会尝试删除C:\inetpub\wwwroot\WebApp1\images\kurum
而不是C:\inetpub\wwwroot\WebApp1\images\kurum\oldfile.jpg
,因此不会删除任何内容。
我已经检查了文件夹和应用程序的权限,它们没问题。我用谷歌搜索,抨击,诽谤,审判和审讯错误但没有成功。我现在差不多两天了:(
我做错了什么?有什么想法吗?
提前致谢。
答案 0 :(得分:1)
在causita评论之后,我使用了索引作为值。它适用于除KurumLogoPath
列以外的所有列。在尝试查找列索引时,我意识到没有索引号与KurumLogoPath
列相关。然后我意识到索引来自&lt;%#Bind(&#34; ColumName&#34;)%&gt;语句..但FileUpload控件并没有像文本框或隐藏字段那样具有数据绑定的性质。所以我在它下面添加了一个隐藏字段:
<asp:FileUpload ID="KurumLogoPathFUU" runat="server" />
<asp:HiddenField ID="KurumLogoPathHF" Value='<%# Bind("KurumLogoPath") %>' runat="server" />
然后e.OldValues("KurumLogoPath")
和e.OldValues(2)
以及文件删除过程都运行良好。