从AsyncFileUpload uploadCompleted事件内部更新网格

时间:2010-02-10 15:47:34

标签: c# asp.net ajax file-upload

我有以下代码,我无法让UpdatePanel更新。有任何想法吗?我想知道是否可以在异步方法中进行更新?

<asp:UpdatePanel ID="UpdatePanel2" runat="server" UpdateMode="Conditional">
              <ContentTemplate>
                 <asp:DataGrid ID="dgFiles" runat="server" DataKeyField="ConfigurationFileId" AutoGenerateColumns="false" GridLines="None" 
                 OnItemCommand="dgFiles_ItemCommand" Width="40%">
                <Columns>
                    <asp:TemplateColumn>
                        <ItemTemplate>
                            <asp:Label ID="lblFile" runat="server" Text='<%#Eval("SourceFileName") %>'></asp:Label>
                        </ItemTemplate>
                    </asp:TemplateColumn>
                    <asp:TemplateColumn>
                        <ItemTemplate>
                            <asp:LinkButton ID="hypDelete" runat="server" Text="Delete" CssClass="blue" CommandName="Delete" CommandArgument="5"></asp:LinkButton>
                        </ItemTemplate>
                    </asp:TemplateColumn>
                </Columns>
               </asp:DataGrid>
              </ContentTemplate>
           </asp:UpdatePanel>
           <asp:UpdatePanel ID="UpdatePanel3" runat="server" UpdateMode="Conditional">
              <ContentTemplate>
                 <ajaxToolkit:AsyncFileUpload ID="Upload1" runat="server" UploaderStyle="Modern" Width="400px"
                     Style="visibility: hidden" OnUploadedComplete="Upload1_Complete" OnClientUploadComplete="UploadComplete" />

                 <asp:DataGrid ID="dgAddFiles" runat="server" DataKeyField="ConfigurationFileId" AutoGenerateColumns="false"
                    Width="40%" Style="visibility: hidden">
                       <Columns>
                          <asp:TemplateColumn>
                             <ItemTemplate>
                                <asp:Label ID="lblFile" runat="server" Text='<%#Eval("SourceFileName") %>'></asp:Label>
                             </ItemTemplate>
                          </asp:TemplateColumn>
                          <asp:TemplateColumn>
                             <ItemTemplate>
                                <asp:LinkButton ID="hypDelete" runat="server" Text="Delete" CssClass="blue" CommandName="Delete" CommandArgument="5"></asp:LinkButton>
                             </ItemTemplate>
                          </asp:TemplateColumn>
                       </Columns>
                    </asp:DataGrid>
              </ContentTemplate>
           </asp:UpdatePanel>




 protected void Upload1_Complete(object sender, AjaxControlToolkit.AsyncFileUploadEventArgs e)
  {
     if (Upload1.HasFile)
     {
        Upload1.SaveAs(Server.MapPath("~/files/temp/") + Upload1.FileName);
        byte[] data = Upload1.FileBytes;
        var file = new Files();
        file.FileId = Guid.NewGuid();
        file.FileName = Upload1.FileName;
        file.Data = data;

        using (var ctx = new DeviceManagerEntities())
        {
           ctx.AddToFiles(file);

           ctx.SaveChanges();

           var files = ctx.Files
              .ToList();
           dgAddFiles.DataSource = files;
           dgAddFiles.DataBind();
           UpdatePanel3.Update();
        }
     }
  }

1 个答案:

答案 0 :(得分:3)

阅读this (Credit to Schelfaut.NET),看看它是否能解决您的问题: