Reloading GridView or Repeater on AjaxFileUpload UploadComplete Event in ASP.NET
Hi, anyway, to update (Gridview or Repeater data) after AjaxFileUpload UploadComplete Event. I want to be able to load multiple images using AjaxFileUpload, and once the files are loaded, it should display those images as a GridView or Repeater control.
I could not have done this unless the button click button was clicked.
Any ideas ???
Place the hidden button on the form and attach this function to OnClientUploadComplete
the expander event handler
<asp:Button runat="server" ID="HiddenButton" OnClick="RefreshGridView" style="display:none;" />
function uploadComplete(sender, args) {
var done = true;
for (var index = 0; index < sender._filesInQueue.length; ++index) {
if (!sender._filesInQueue[index]._isUploaded) {
return;
}
}
__doPostBack("<%= HiddenButton.UniqueID %>", "");
})
then update the GridView when this button is clicked.
This code checks the uploaded file, creates an email with information about the file, sends an email to the person for whom the file with the link was intended. It also stores all information in the database. The download page displays a grid listing all the files that have been downloaded. It is updated after downloading the files. I think you can get what you need.
Partial Class upload_Default
Inherits System.Web.UI.Page
Protected Sub UploadButton2_Click(sender As Object, e As EventArgs)
Dim fileGuid As String
fileGuid = Guid.NewGuid.ToString
If AsyncFileUpload1.HasFile Then
If AsyncFileUpload1.FileContent.Length < 20971500 Then
Try
Dim fileSizeB As Integer = AsyncFileUpload1.PostedFile.ContentLength
Dim fileSize = fileSizeB / 1024
Dim filename As String = Path.GetFileName(AsyncFileUpload1.FileName)
Dim fileNameTwo As String = Trim(fileGuid) + Trim(filename)
Dim ExistPdfFilenamOPO As String
ExistPdfFilenamOPO = Server.MapPath("~/uploads/files/") & filename
If File.Exists(ExistPdfFilenamOPO) Then
Label2.Text = "File is already there"
Else
Dim saveDir As String = "\Uploads\files\"
Dim appPath As String = Request.PhysicalApplicationPath
Dim savePath As String = appPath + saveDir + _
Server.HtmlEncode(AsyncFileUpload1.FileName)
AsyncFileUpload1.SaveAs(savePath)
UploadStatusLabel2.Text = "Upload status: File uploaded."
Label2.Text = ""
' Email
Dim sr As New StreamReader(appPath & "EmailTemplates/FileUpload.htm")
Dim FName As String = TextBoxFName.Text
Dim LName As String = TextBoxLName.Text
Dim Email As String = TextBoxEmail.Text
Dim fullPath As String
fullPath = "https://website.com/uploads/default.aspx?fileGuid=" + fileGuid
Dim message As New MailMessage()
message.IsBodyHtml = True
message.From = New MailAddress("Your email")
message.[To].Add(New MailAddress(Email))
message.Subject = "The file you requested from SRTR"
message.Body = sr.ReadToEnd()
sr.Close()
message.Body = message.Body.Replace("<%FName%>", FName)
message.Body = message.Body.Replace("<%LName%>", LName)
message.Body = message.Body.Replace("<%Email%>", Email)
message.Body = message.Body.Replace("<%FileName%>", filename)
message.Body = message.Body.Replace("<%VerificationUrl%>", fullPath)
Dim client As New SmtpClient()
client.Send(message)
'Insert in to t_UploadFiles
Dim datenow As Date = System.DateTime.Now()
Dim ExDate As Date = datenow.AddDays(15)
Dim Downloaded As Boolean = False
Dim connectionString As String = ConfigurationManager.ConnectionStrings("ConnectionString").ConnectionString
Dim updateSql As String = "INSERT t_UploadFiles (FileGuid, FileName, FileSize, FName, LName, Email, UploadDate, ExDate, Downloaded) SELECT @FileGuid, @FileName, @FileSize, @FName, @LName, @Email, @UploadDate, @ExDate, @Downloaded"
Using myConnection As New SqlConnection(connectionString)
myConnection.Open()
Dim myCommand As New SqlCommand(updateSql, myConnection)
myCommand.Parameters.AddWithValue("@FileGuid", fileGuid.Trim())
myCommand.Parameters.AddWithValue("@FileName", filename.Trim())
myCommand.Parameters.AddWithValue("@FileSize", fileSize)
myCommand.Parameters.AddWithValue("@FName", FName.Trim())
myCommand.Parameters.AddWithValue("@LName", LName.Trim())
myCommand.Parameters.AddWithValue("@Email", Email)
myCommand.Parameters.AddWithValue("@UploadDate", datenow)
myCommand.Parameters.AddWithValue("@ExDate", ExDate)
myCommand.Parameters.AddWithValue("@Downloaded", Downloaded)
myCommand.ExecuteNonQuery()
myConnection.Close()
End Using
articleListXX.DataBind()
End If
Catch ex As Exception
UploadStatusLabel2.Text = "Upload status: The file could not be uploaded.<br/>The following error occured: " + ex.Message
End Try
Else
UploadStatusLabel2.Text = "File is too large."
End If
Else
UploadStatusLabel2.Text = "You did not specify a file to upload."
End If
End Sub
End Class
Ok guys, thanks for your input and sorry for making my request a little unclear. I finally figured it out, but only because of your ideas. Here is my code. The same goes for the gridview. The main purpose is to use the AjaxFileUpload control to load the images, and when called OnUploadComplete="AjaxFileUpload1_UploadComplete"
, the method to create the thumbnail image. When thumbnails are created, the method populatePic()
is a call to populate thumbnail images in a relay control using the javascript _doPostBack () method without the user having to trigger the button.
<script type="text/javascript">
function showUploadedPic()
{
__doPostBack('btnAdd', null);
}
</script>
<cc1:AjaxFileUpload ID="AjaxFileUpload1" runat="server" OnUploadComplete="AjaxFileUpload1_UploadComplete" ThrobberID="myThrobber" MaximumNumberOfFiles="10" AllowedFileTypes="jpg,jpeg" OnClientUploadComplete="showUploadedPic" />
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<asp:Repeater ID="Repeater1" runat="server">
<ItemTemplate>
<asp:Image ID="Image1" runat="server" ImageUrl="<%# Container.DataItem %>" height="100"/>
</ItemTemplate>
</asp:Repeater>
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="btnAdd" EventName="Click" />
</Triggers>
</asp:UpdatePanel>
Code for
protected void btnAdd_Click(object sender, EventArgs e)
{
populatePic();
}
protected void AjaxFileUpload1_UploadComplete(object sender, AjaxControlToolkit.AjaxFileUploadEventArgs e)
{
string filePath = Server.MapPath("~/files/") + e.FileName;
AjaxFileUpload1.SaveAs(filePath);
createThumbnail();
}
Below code is tested and works.
<asp:Button runat="server" ID="HiddenButtonFileUpload"
OnClick = "RefreshGridView" style = "display: none;" / ">
<ajax:AjaxFileUpload ID="AjaxFileUpload11" runat="server"
MaximumNumberOfFiles="3" AllowedFileTypes="txt,xls,xlsx,doc,docx,pdf"
Width="400px"
OnUploadComplete="OnUploadComplete"
OnClientUploadStart="UploadStart" OnClientUploadCompleteAll="UploadComplete"
ClearFileListAfterUpload="true" />
function UploadComplete() {
unblock();
__doPostBack("<%= HiddenButtonFileUpload.UniqueID %>", "");
}
Code Behind :
protected void RefreshGridView(object sender, EventArgs e)
{
BindForm(); // refresh your gridview
}