I have a FormView on a page in my asp.net 4.5 VB application. I have a FileUpload in EditItemTemplate
and InsertItemTemplate
of my FormView. InsertItemTemplate
works fine as expected. I want my FormView to:
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" />
Related 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
I can see MyPath
but not e.NewValues("KurumLogoPath")
or e.OldValues("KurumLogoPath")
I also tried e.NewValues.Item("KurumLogoPath")
and e.NewValues.Item("KurumLogoPath")
with no success. So I think that all e.OldValues
and e.NewValues
are null.. Thus file.delete
tries to delete C:\inetpub\wwwroot\WebApp1\images\kurum
instead of C:\inetpub\wwwroot\WebApp1\images\kurum\oldfile.jpg
as a result deletes nothing.
I have checked permissions for the folder and Application, they are OK. I have googled, binged, yahooed, trialed & errored with no success. It's been about two days now that I am on this :(
What am I doing wrong? Any ideas?
Thanks in advance.
After causita's comment I used index for the values. It worked for all columns except for KurumLogoPath
column. While trying to find that columns index I realized that no index number was related to KurumLogoPath
column. Then I realized indices are coming from <%# Bind("ColumName") %> statements.. But FileUpload control doesn't have databinding in it's nature just like a textbox or a hiddenfield did. So I added a hiddenfield under it:
<asp:FileUpload ID="KurumLogoPathFUU" runat="server" />
<asp:HiddenField ID="KurumLogoPathHF" Value='<%# Bind("KurumLogoPath") %>' runat="server" />
and then both e.OldValues("KurumLogoPath")
and e.OldValues(2)
and file deleting process worked very well.