locked
How to Bind DropDownList on Uploadcomplete Event of Ajax Asynchronous File Upload Control RRS feed

  • Question

  • User1552454131 posted

    Hello Firends,

                     i am using Ajax asynchronous file upload control to upload excel file , so that it should be imported, the issue i am facing is i want the list of sheet from uploaded Excel  in dropdownlist , for this i am using uploadedcomplete event of  Ajax asynchronous file upload, in this i am retreving the sheet names of the uploaded sheet in a datatable but not able to bind it on dropdownlist, can anbody help me with this.

    Thanks and Regards,

    Harnek

    Thursday, January 16, 2014 4:38 AM

Answers

  • User1734617369 posted

    Hi,

    The problem is that the event OnUploadedComplete is not part of the standard page lifecycle so even if you assign a datasource to the DropDownList in that event this will not update the DropDownList since it won't be rendered again, on way of doing it is like in this example, tha page:

    <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm2.aspx.cs" Inherits="WebApplication1.WebForm2" %>
    
    <%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="asp" %>
    
    <!DOCTYPE html>
    
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title></title>
        <script>
            function AddFileNameToList(sender, args) {
                <%=ClientScript.GetPostBackEventReference(UP1, "" )%>;
            }
        </script>
    </head>
    <body>
        <form id="form1" runat="server">
            <asp:ToolkitScriptManager ID="ToolkitScriptManager1" runat="server"></asp:ToolkitScriptManager>
            <asp:AsyncFileUpload ID="AsyncFileUpload1" runat="server" OnClientUploadComplete="AddFileNameToList" OnUploadedComplete="AsyncFileUpload1_OnUploadedComplete" />        
            <asp:UpdatePanel runat="server" ID="UP1">
                <ContentTemplate>
                    <asp:DropDownList runat="server" ID="FileList" AutoPostBack="True" />
                </ContentTemplate>
            </asp:UpdatePanel>        
        </form>
    </body>
    </html>

    and the code:

    using System.Collections.Generic;
    using AjaxControlToolkit;
    using System;
    
    namespace WebApplication1
    {
        public partial class WebForm2 : System.Web.UI.Page
        {
            protected void Page_Load(object sender, EventArgs e)
            {
                if (IsPostBack)
                {
                    FileList.Items.Clear();
                    FileList.DataSource = Session["SheetNames"] != null
                        ? (List<string>) Session["SheetNames"]
                        : new List<string>();
                    FileList.DataBind();
                }
            }
    
            protected void AsyncFileUpload1_OnUploadedComplete(object sender, AsyncFileUploadEventArgs e)
            {
                if (Session["SheetNames"] != null)
                {
                    ((List<string>)Session["SheetNames"]).Add(e.FileName);
                }
                else
                {
                    Session["SheetNames"] = new List<string> {e.FileName};
                }              
            }
        }
    }

    in your case you can assign the shhet names to the session variable and use that instead. This may not be the most elegant solution but should work if you adds some error checking and such.

    Best regards
    Johan

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, January 16, 2014 6:51 AM