Där det står
"'You would want to rebind the Directory's files to the DataGrid after
'deleting the file..."
måste du lägga in funktionaliteten för att binda din DataGrid igen.
4 svar · 343 visningar · startad av Griphem
Jag har hittat ett fint script som bygger en datagrid från filer i en mapp - det fungerar utmärkt men...
... när man har tryckt på delete så måste man ladda om sidan manuellt för att se resultatet. Vad är det jag glömt?
<%@ Import Namespace="System.IO" %>
<script language="VB" runat="server">
Sub Page_Load(sender as Object, e as EventArgs)
If Not Page.IsPostBack then
Dim dirInfo as New DirectoryInfo(Server.MapPath(""))
articleList.DataSource = dirInfo.GetFiles("*.pdf")
articleList.DataBind()
End If
End Sub
Sub articleList_ItemDataBound(sender as Object, e as DataGridItemEventArgs)
' First, make sure we're NOT dealing with a Header or Footer row
If e.Item.ItemType <> ListItemType.Header AND _
e.Item.ItemType <> ListItemType.Footer then
'Now, reference the Button control that the Delete ButtonColumn
'has been rendered to
Dim deleteButton as Button = e.Item.Cells(0).Controls(0)
'We can now add the onclick event handler
deleteButton.Attributes("onclick") = "javascript:return " & _
"confirm('Are you sure you want to delete the file " & _
DataBinder.Eval(e.Item.DataItem, "Name") & "?')"
End If
End Sub
Sub articleList_DeleteFile(sender as Object, e as DataGridCommandEventArgs)
'First, get the filename to delete
Dim fileName as String = articleList.DataKeys(e.Item.ItemIndex)
File.Delete(fileName)
'lblMessage.Text = "You opted to delete the file " & _
'fileName & ".<br />" & _
'"This file could be deleted by calling: " & _
'"<code>File.Delete(fileName)</code><p>"
'You would want to rebind the Directory's files to the DataGrid after
'deleting the file...
End Sub
</script>
:D Nicklas
Där det står
"'You would want to rebind the Directory's files to the DataGrid after
'deleting the file..."
måste du lägga in funktionaliteten för att binda din DataGrid igen.
I din Page_Load databinder du med
Dim dirInfo as New DirectoryInfo(Server.MapPath(""))
articleList.DataSource = dirInfo.GetFiles("*.pdf")
articleList.DataBind()
Använd samma tre rader sist i din delete funktion.
Man kan även lägga de tre raderna i en egen funktion, t.ex. BindGrid() och ropa på den funktionen både i Page_Load och Delete.