Jack
Jack

Reputation: 161

Reload GridView or Repeater on AjaxFileUpload UploadComplete Event in ASP.NET

Hi is there anyway to update (Gridview or Repeater data) after AjaxFileUpload UploadComplete Event. What I want to do is upload multiple picture using AjaxFileUpload and once the files are uploaded it should display those pictures into a GridView or Repeater control.

I could not do this unless a button click event is fired.

Any ideas???

Upvotes: 3

Views: 5119

Answers (4)

IUnknown
IUnknown

Reputation: 22478

Put hidden button onto a form and attach this function to OnClientUploadComplete event handler of extender

<asp:Button runat="server" ID="HiddenButton" OnClick="RefreshGridView" style="display:none;" />

function uploadComplete(sender, args) {
    for (var index = 0; index < sender._filesInQueue.length; ++index) {
        if (!sender._filesInQueue[index]._isUploaded) {
            return;
        }
    }
    __doPostBack("<%= HiddenButton.UniqueID %>", "");
})

then, refresh your GridView on this button's click.

Upvotes: 4

suresh
suresh

Reputation: 1

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
    }

Upvotes: 0

Jack
Jack

Reputation: 161

Ok guys thanks for your contribution and sorry for making my query a bit unclear. I finally figured it out but only because of your ideas. Here is my code. The same applies to gridview. The main purpose is to use AjaxFileUpload control to upload pictures and on OnUploadComplete="AjaxFileUpload1_UploadComplete" event call the method for creating thumbnail image. Once thumbnails are created the method populatePic() is call to populate thumbnail images into a repeater control with the help of javascript _doPostBack() method without the needs of firing a button by the user.

<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 Behind

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();         
    }  

Upvotes: 0

Eric
Eric

Reputation: 23

This code checks for the file that's uploaded, creates an email with the file information, emails the person the file was intended for with a link. It also stores all the info into a database. On the upload page is a gridview that lists all the files that have been uploaded. It updates after the files is loaded. I think you can get what you need out of it.

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

Upvotes: 1

Related Questions