Hi rakeshkuma,
AsyncFileUpload uploads files asnychronously, so the event at code behind is not triggered as that were in a normal postback.
You need to use OnClientUploadComplete event to bind the GridView.
Refer below sample.
HTML
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<asp:UpdatePanel runat="server">
<ContentTemplate>
<ajax:AsyncFileUpload ID="AsyncFileUpload1" CssClass="form-control" CompleteBackColor="White"
Width="100%" runat="server" UploaderStyle="Modern" UploadingBackColor="White" ThrobberID="imgLoad"
OnUploadedComplete="AsyncFileUpload1_UploadedComplete" OnClientUploadComplete="uploadComplete" />
<asp:Image ID="imgLoad" runat="server" ImageUrl="~/images/loader.gif" />
<asp:Label ID="Label38" runat="server" Font-Size="12px" ForeColor="Green"></asp:Label>
<asp:Label ID="Label39" runat="server" Font-Size="12px" ForeColor="Red"></asp:Label>
<asp:GridView runat="server" ID="gvFiles" AutoGenerateColumns="false" EmptyDataText="No files uploaded">
<Columns>
<asp:BoundField DataField="Text" HeaderText="File Name" />
<asp:TemplateField>
<ItemTemplate>
<asp:LinkButton ID="lnkDownload" Text="Download" CommandArgument='<%# Eval("Value") %>'
runat="server" OnClick="DownloadFile"></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:LinkButton ID="lnkDelete" Text="Delete" CommandArgument='<%# Eval("Value") %>'
runat="server" OnClick="DeleteFile" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<asp:Button ID="btnRefresh" Text="" Style="display: none;" runat="server" OnClick="OnRefresh" />
</ContentTemplate>
</asp:UpdatePanel>
<script type="text/javascript">
function uploadComplete(sender) {
document.getElementById('btnRefresh').click();
}
</script>
Namespaces
C#
using System.Collections.Generic;
using System.IO;
using AjaxControlToolkit;
VB.Net
Imports System.Collections.Generic
Imports System.IO
Imports AjaxControlToolkit
Code
C#
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BindGridView();
}
}
protected void AsyncFileUpload1_UploadedComplete(object sender, AsyncFileUploadEventArgs e)
{
string extension = Path.GetExtension(e.FileName);
if (extension.ToLower() == ".pdf")
{
AsyncFileUpload1.SaveAs(Server.MapPath("documents/") + Path.GetFileName(e.FileName));
}
ClearContents(sender as Control);
}
protected void DownloadFile(object sender, EventArgs e)
{
string filePath = (sender as LinkButton).CommandArgument;
Response.ContentType = ContentType;
Response.AppendHeader("Content-Disposition", "attachment; filename=" + Path.GetFileName(filePath));
Response.WriteFile(filePath);
Response.End();
}
protected void DeleteFile(object sender, EventArgs e)
{
string filePath = (sender as LinkButton).CommandArgument;
File.Delete(filePath);
Response.Redirect(Request.Url.AbsoluteUri);
}
protected void OnRefresh(object sender, EventArgs e)
{
BindGridView();
}
private void BindGridView()
{
string[] filePaths = Directory.GetFiles(Server.MapPath("~/documents/"));
List<ListItem> files = new List<ListItem>();
foreach (string filePath in filePaths)
{
files.Add(new ListItem(Path.GetFileName(filePath), filePath));
}
gvFiles.DataSource = files;
gvFiles.DataBind();
}
private void ClearContents(Control control)
{
for (var i = 0; i < Session.Keys.Count; i++)
{
if (Session.Keys[i].Contains(control.ClientID))
{
Session.Remove(Session.Keys[i]);
break;
}
}
}
VB.Net
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
If Not IsPostBack Then
BindGridView()
End If
End Sub
Protected Sub AsyncFileUpload1_UploadedComplete(ByVal sender As Object, ByVal e As AsyncFileUploadEventArgs)
Dim extension As String = Path.GetExtension(e.FileName)
If extension.ToLower() = ".pdf" Then
AsyncFileUpload1.SaveAs(Server.MapPath("documents/") + Path.GetFileName(e.FileName))
End If
ClearContents(TryCast(sender, Control))
End Sub
Protected Sub DownloadFile(ByVal sender As Object, ByVal e As EventArgs)
Dim filePath As String = TryCast(sender, LinkButton).CommandArgument
Response.ContentType = ContentType
Response.AppendHeader("Content-Disposition", "attachment; filename=" & Path.GetFileName(filePath))
Response.WriteFile(filePath)
Response.End()
End Sub
Protected Sub DeleteFile(ByVal sender As Object, ByVal e As EventArgs)
Dim filePath As String = TryCast(sender, LinkButton).CommandArgument
File.Delete(filePath)
Response.Redirect(Request.Url.AbsoluteUri)
End Sub
Protected Sub OnRefresh(ByVal sender As Object, ByVal e As EventArgs)
BindGridView()
End Sub
Private Sub BindGridView()
Dim filePaths As String() = Directory.GetFiles(Server.MapPath("~/documents/"))
Dim files As List(Of ListItem) = New List(Of ListItem)()
For Each filePath As String In filePaths
files.Add(New ListItem(Path.GetFileName(filePath), filePath))
Next
gvFiles.DataSource = files
gvFiles.DataBind()
End Sub
Private Sub ClearContents(ByVal control As Control)
For i = 0 To Session.Keys.Count - 1
If Session.Keys(i).Contains(control.ClientID) Then
Session.Remove(Session.Keys(i))
Exit For
End If
Next
End Sub
Screenshot