locked
asyncfileupload RRS feed

  • Question

  • User-140674451 posted

    I am using an asynfileupload to save data to a database. After the data is saved into the database . I need to update an updatepanel .

    <head runat="server">
        <title></title>
        <script type ="text/javascript" >
            function UploadComplete(sender, args) {
                
            __doPostBack('UpdatePanel1', '');  
            }

       </script>
    </head>
    <body>
        <form id="form1" runat="server">  <asp:ToolkitScriptManager ID="ToolkitScriptManager1" runat="server">
        </asp:ToolkitScriptManager>
        <div>
        
        </div>
        <asp:UpdatePanel   ID="UpdatePanel1" UpdateMode ="Always"  runat="server">
        <ContentTemplate >
           
         <asp:Label ID="Label1" runat="server" Text ="1" Visible ="true"  ></asp:Label></ContentTemplate>
        </asp:UpdatePanel>
       
        <asp:AsyncFileUpload ID="AsyncFileUpload1" runat="server"
            OnClientUploadComplete="UploadComplete" onuploadedcomplete="AsyncFileUpload1_UploadedComplete"
             />
     
        </form>
    </body>

    protected void AsyncFileUpload1_UploadedComplete(object sender, AjaxControlToolkit.AsyncFileUploadEventArgs e)
        {
            string savePath = MapPath("~/ExcelUpload/" + "1.xls");

            AsyncFileUpload1.SaveAs(savePath);
            UpdateDataBase();
            Label1.Text = "saved";
            
        }

    As you can see the Label1.Text is modified in code behind but that is not reflected on the page. How can I do this?

    Friday, October 1, 2010 5:05 PM

Answers

  • User78336405 posted

    Hi,

    Please change your code as below. I have tested, and it is running normal. 

    <head runat="server">
        <title></title>
        <script type ="text/javascript" >
            function UploadComplete(sender, args) {
                
            __doPostBack('UP_Btn', '');  
            }
    
       </script>
    </head>
    <body>
        <form id="form1" runat="server">  <asp:ToolkitScriptManager ID="ToolkitScriptManager1" runat="server">
        </asp:ToolkitScriptManager>
        <div>
        
        </div>
        <asp:Button ID="UP_Btn" runat="server" Text="Updatepanel_Trigger" onclick="UP_Btn_Click" style="display:none" />
        <asp:UpdatePanel   ID="UpdatePanel1" UpdateMode="Conditional" runat="server">
        <ContentTemplate >
           
         <asp:Label ID="Label1" runat="server" Text ="1" Visible ="true"  ></asp:Label></ContentTemplate>
        </asp:UpdatePanel>
       
        <asp:AsyncFileUpload ID="AsyncFileUpload1" runat="server" 
            OnClientUploadComplete="UploadComplete" onuploadedcomplete="AsyncFileUpload1_UploadedComplete"
             />
      
        </form>
    </body>


     

    protected void AsyncFileUpload1_UploadedComplete(object sender, AjaxControlToolkit.AsyncFileUploadEventArgs e)
        {
            string savePath = MapPath("~/ExcelUpload/" + "1.xls");
    
            AsyncFileUpload1.SaveAs(savePath);
            UpdateDataBase();
            Label1.Text = "saved";
            
        }
    
        protected void UP_Btn_Click(object sender, EventArgs e)
        {
            UpdatePanel1.Update();
        }


     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, October 5, 2010 4:34 AM

All replies

  • User1372589710 posted

    The first thing I'm noticing is that you don't have any triggers defined on your update panel.

    Below the "ContentTemplate" tag, add a "Triggers" tag like below. You might also want to modify the UpdatePanel's attributes at indicated below, this will provide greater control over when the update panel does it's server-request and improve performance by preventing it from updating when it doesn't need to. (I am assuming, of course, that your page is a bit more complex than what is listed here)


    <asp:UpdatePanel ID="..." runat="server" ChildrenAsTriggers="false" UpdateMode="Conditional">
    <ContentTemplate>
    ....
    </ContentTemplate>
    <Triggers>
    <asp:AsyncPostback Control="AsyncFileUpload1" Event="UploadedComplete" />
    </Triggers>
    </asp:UpdatePanel>


    Another thing to be aware of is that I have seen many post (and had some issues personally) with the AsyncFileUpload control not triggering the "UploadComplete" event on the server. You might want to put a breakpoint in and make sure that this postback in occurring.

    If it's not, you can get around this problem by using the "OnClientUploadComplete" property and pointing it at some javascript function that will "fake" a postback.


    Friday, October 1, 2010 7:18 PM
  • User78336405 posted

    Hi,

    Please change your code as below. I have tested, and it is running normal. 

    <head runat="server">
        <title></title>
        <script type ="text/javascript" >
            function UploadComplete(sender, args) {
                
            __doPostBack('UP_Btn', '');  
            }
    
       </script>
    </head>
    <body>
        <form id="form1" runat="server">  <asp:ToolkitScriptManager ID="ToolkitScriptManager1" runat="server">
        </asp:ToolkitScriptManager>
        <div>
        
        </div>
        <asp:Button ID="UP_Btn" runat="server" Text="Updatepanel_Trigger" onclick="UP_Btn_Click" style="display:none" />
        <asp:UpdatePanel   ID="UpdatePanel1" UpdateMode="Conditional" runat="server">
        <ContentTemplate >
           
         <asp:Label ID="Label1" runat="server" Text ="1" Visible ="true"  ></asp:Label></ContentTemplate>
        </asp:UpdatePanel>
       
        <asp:AsyncFileUpload ID="AsyncFileUpload1" runat="server" 
            OnClientUploadComplete="UploadComplete" onuploadedcomplete="AsyncFileUpload1_UploadedComplete"
             />
      
        </form>
    </body>


     

    protected void AsyncFileUpload1_UploadedComplete(object sender, AjaxControlToolkit.AsyncFileUploadEventArgs e)
        {
            string savePath = MapPath("~/ExcelUpload/" + "1.xls");
    
            AsyncFileUpload1.SaveAs(savePath);
            UpdateDataBase();
            Label1.Text = "saved";
            
        }
    
        protected void UP_Btn_Click(object sender, EventArgs e)
        {
            UpdatePanel1.Update();
        }


     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, October 5, 2010 4:34 AM
  • User2059472900 posted

    hi



    i tried your code, but now it runs twice into the "AsyncFileUpload1_UploadedComplete" Sub...

    So my Image will be saved twice...


    how can i prevent this?


    thanx! :)

    Wednesday, October 6, 2010 4:22 PM
  • User78336405 posted

    Hi,

    The "AsyncFileUpload1_UploadedComplete" Sub not run twice. Please test again. I have tested on my side. Everything is OK.

    Wednesday, October 6, 2010 10:08 PM
  • User2059472900 posted

    ok ill show you my code :)

    <asp:content id="Content1" contentplaceholderid="head" runat="Server">

    <%@ Page Title="" Language="VB" MasterPageFile="~/Master.master" AutoEventWireup="false" CodeFile="addImages.aspx.vb" Inherits="addImages" %>
    
    <%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="asp" %>
    
    <asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">
    
        <link rel="stylesheet" type="text/css" href="App_Themes/default/addImages.css" />
    
    
        <script type="text/javascript">
    
            function UploadComplete(sender, args) {
                __doPostBack("<%=btnUploadcompleted.ClientID %>", '');  
            }  
    
        </script>
    
    </asp:Content>
    
    <asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
        <asp:ScriptManager ID="ScriptManager1" runat="server">
        </asp:ScriptManager>
              
        <asp:AsyncFileUpload ID="fileUpload" runat="server" 
                    CompleteBackColor="#99FF99" UploadingBackColor="#66CCFF" 
                    ErrorBackColor="#FF3300" ThrobberID="imgUploadPrograss" 
                    onclientuploadcomplete="UploadComplete" />
    
        <asp:UpdatePanel ID="UpdatePanel1" runat="server">
            <ContentTemplate>           
                <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
            </ContentTemplate> 
        </asp:UpdatePanel>
    
        <asp:Button ID="btnUploadcompleted" runat="server" Text="UpdatePanelUpdater" />
    
        <asp:Image ID="imgUploadPrograss" runat="server" 
            ImageUrl="~/App_Themes/default/images/ajax-loader.gif" />
       
    </asp:Content>

    </asp:content>


    <asp:content id="Content1" contentplaceholderid="head" runat="Server">

      Protected Sub AsyncFileUpload1_UploadedComplete(ByVal sender As Object, ByVal e As AjaxControlToolkit.AsyncFileUploadEventArgs) Handles fileUpload.UploadedComplete
    
    
            If Me.fileUpload.HasFile = True Then
    
                Dim folder As String = "57aa12cde175487a975d3499cb3c4bd9"
    
                Me.fileUpload.SaveAs(Server.MapPath("Uploads/") & folder & "/" & Me.fileUpload.PostedFile.FileName)
    
                Me.Label1.Text = "uploaded"
    
            End If
        End Sub
    
    
        Protected Sub btnUploadcompleted_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnUploadcompleted.Click
            Me.UpdatePanel1.Update()
        End Sub

    <mce:script type="mce-text/javascript"><!-- function UploadComplete(sender, args) { __doPostBack("<%=btnUploadcompleted.ClientID %>", ''); } // --></mce:script> </asp:content> <asp:scriptmanager id="ScriptManager1" runat="server"> </asp:scriptmanager> <contenttemplate> <asp:label id="Label1" runat="server" text="Label"></asp:label> </contenttemplate>

    <asp:content id="Content2" contentplaceholderid="ContentPlaceHolder1" runat="Server"><asp:asyncfileupload id="fileUpload" runat="server" completebackcolor="#99FF99" uploadingbackcolor="#66CCFF" errorbackcolor="#FF3300" throbberid="imgUploadPrograss" onclientuploadcomplete="UploadComplete"><asp:updatepanel id="UpdatePanel1" runat="server"> </asp:updatepanel> <asp:button id="btnUploadcompleted" runat="server" text="UpdatePanelUpdater"> <asp:image id="imgUploadPrograss" runat="server" imageurl="~/App_Themes/default/images/ajax-loader.gif"> </asp:image></asp:button></asp:asyncfileupload></asp:content>

    Thursday, October 7, 2010 12:55 PM