在ASP.NET中的AjaxFileUpload UploadComplete事件上重新加载GridView或Repeater

时间:2013-02-12 15:25:24

标签: asp.net ajax file-upload

在AjaxFileUpload UploadComplete Event之后,无论如何都要更新(Gridview或Repeater数据)。我想要做的是使用AjaxFileUpload上传多张图片,一旦文件上传,它应该将这些图片显示到GridView或Repeater控件中。

除非触发按钮点击事件,否则无法执行此操作。

任何想法???

4 个答案:

答案 0 :(得分:3)

将隐藏按钮放在表单上,​​并将此函数附加到扩展程序的OnClientUploadComplete事件处理程序

<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 %>", "");
})

然后,在此按钮的单击上刷新GridView。

答案 1 :(得分:1)

此代码检查上传的文件,创建包含文件信息的电子邮件,通过链接向该文件所针对的人发送电子邮件。它还将所有信息存储到数据库中。在上传页面上是一个gridview,列出了已上传的所有文件。它在加载文件后更新。我想你可以从中获得所需的东西。

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

答案 2 :(得分:0)

好的家伙感谢您的贡献,并对不起让我的查询有点不清楚。我终于弄清楚了,但这只是因为你的想法。这是我的代码。这同样适用于gridview。主要目的是使用AjaxFileUpload控件上传图片,并在OnUploadComplete="AjaxFileUpload1_UploadComplete"事件调用上创建缩略图图像的方法。创建缩略图后,调用方法populatePic(),在javascript _doPostBack()方法的帮助下将缩略图图像填充到转发器控件中,而无需用户触发按钮。

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

代码背后

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

答案 3 :(得分:0)

以下代码经过测试并有效。

<asp:Button runat="server" ID="HiddenButtonFileUpload" 

的OnClick =&#34; RefreshGridView&#34;风格=&#34;显示:无;&#34; /&GT;

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