【问题标题】:Reload GridView or Repeater on AjaxFileUpload UploadComplete Event in ASP.NET在 ASP.NET 中的 AjaxFileUpload UploadComplete 事件上重新加载 GridView 或中继器
【发布时间】:2013-01-27 22:55:55
【问题描述】:

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

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

有什么想法吗???

【问题讨论】:

标签: asp.net ajax file-upload


【解决方案1】:

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

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

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

【讨论】:

    【解决方案2】:

    此代码检查已上传的文件,创建包含文件信息的电子邮件,通过链接向文件的目标用户发送电子邮件。它还将所有信息存储到数据库中。上传页面上有一个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
    

    【讨论】:

      【解决方案3】:

      好的,谢谢你们的贡献,很抱歉让我的查询有点不清楚。我终于弄明白了,但这只是因为你的想法。这是我的代码。这同样适用于网格视图。主要目的是使用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();         
          }  
      

      【讨论】:

        【解决方案4】:

        下面的代码已经过测试并且可以工作。

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

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2012-06-09
          • 2015-04-11
          • 1970-01-01
          • 1970-01-01
          • 2021-12-11
          • 1970-01-01
          • 2013-07-14
          • 1970-01-01
          相关资源
          最近更新 更多