Troubleshooting: AsyncFileUpload functionality not working within Listview's Insert, Edit Itemtemplate, and EmptyData Template

Having trouble with AsyncFileUpload not working inside Listview Insert, Edit Itemtemplate and EmptyData Template.

The following are my Client Side Functions:

function AttachmentUploadSuccessful() {
    debugger;
    var textError = $(".AttachmentError").text();
    if (textError.length > 0) {
        var text = $(".AttachmentError");
        text.innerText = text.textContent = textError;
        sender._onError(textError); // it will raise the OnClientUploadError event
        return;
    } else {

        //alert(" File attachment is uploaded successfully.");
        //CODE TO  REMOVE FILE AND BACKGROUND COLOR OF FILE UPLOADER
        $('.ModelBackgroundforCreateItem').hide();
        $('.PopupPanel').hide();
        var UploadControls = $('#<%= FileUpload.ClientID %> :input');
        UploadControls.each(function () {

            //$(this).val("");
            $(this).css('background-color', '#fff');
        });

        //Updating Update panel by clicking button
        $(".RefreshList").click();
    }
}

 function AttachmentUploadFailed() {

    alert("An error occured while uploading  File Attachment. ");
}

Markup in .aspx file

<asp:ListView ID="ListView2" runat="server">
    <EmptyDataTemplate>
    <table class="fileUpload" runat="server" id="FileUploadID">
                <tr>
                    <td>
                        <div style="width: 350px; overflow-x: hidden;">
                            <asp:AsyncFileUpload runat="server" ID="FileUpload" ThrobberID="Throbber" OnClientUploadError="AttachmentUploadFailed"
                                OnClientUploadComplete="AttachmentUploadSuccessful" UploaderStyle="Traditional" UploadingBackColor="" Style="display: inline-block; margin-top: 5px;"
                                OnUploadedComplete="FileUpload_UploadedComplete">
                            </asp:AsyncFileUpload>
                        </div>
                    </td>
                    <td style="width: 30px">
                        <asp:Image ID="Throbber" ImageUrl="~/Image/AttachmentLoading.gif" Style="display: None; width: 20px;" runat="server" />
                        <br />
                    </td>
                </tr>
        </table>
        </EmptyDataTemplate>
</asp:ListView>

Encountering an issue where the Client side event OnClientUploadError is getting called but causing errors when trying to upload a file inside a Listview. Tried on a simple page and within Update panel where it worked fine, but inside Listview it's throwing client error. Checked similar posts online but couldn't find a solution, need assistance.

How to find Ajaxfileupload control inside Listview When in Editmode

AsyncFileUpload within EditItemTemplate of ListView

Answer №1

Upon reviewing the code you shared, I have identified two issues:

  1. The line of code:
    var UploadControls = $('#<%= FileUpload.ClientID %> :input')
  2. You are missing a ClientIDMode property in your markup, so it defaults to Inherit

Both of these problems stem from using the AsyncFileUploader within a ListView, causing multiple elements with the same FileUpload id on your page.

To address this, you should first modify the function declaration to:

function AttachmentUploadSuccessful(sender, e) { // then you can access the sender

Next, to obtain the input element, utilize:

var UploadControls = $(sender._inputFile);

Lastly, to resolve the second issue, update the ClientIDMode property to

AutoID</code, allowing the <code>AsyncFileUploader
to correctly locate its referenced client components.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Scraping a few URLs with Javascript for Web Data Extraction

I'm struggling to retrieve data from multiple URLs and write it to a CSV file. The problem I'm facing is that the fetched data is not complete (I expect 10 items) and it's not in the correct order. Instead of getting 1, 2, 3 sequentially, I ...

Triggering an event with two clicks in JQuery

I'm facing an issue with firing 2 click events in jQuery to remove files when a RadioBoxList is selected. The buttons on the page are calling the same function as delete_Click in the code behind. Here is my current code: <asp:Button ID="delBtn1" ...

Tips for sending a URL in form-data as a file with JavaScript or Axios

Currently, I am using Vue.js and Axios to send form data to a specific link. However, I encountered an issue where the file is not being sent and I am receiving an error message. The parameter 'file' had the following problems: file transferred w ...

Using Bootstrap Multiselect and making AJAX GET requests with nested objects

I'm having difficulties with retrieving nested objects using $.ajax and dynamically populating Bootstrap Multiselect dropdown select options. This issue is similar to the ones discussed in the following Stack Overflow threads: Issue with Data returnin ...

Guide to hosting AngularJS Material documentation on a local server

After gathering the latest angularjs material, I noticed that the links in the document lead to absolute URLs at material.angularjs.org/.... I wish to have access to read the documentation and demonstration content locally. ...

Connecting a text input in a nested component to the data passed down from its parent component in VueJS

My VueJS setup involves a child component sending data to a parent component, which then tries to route the data to a sibling of the child to create new components. I'm using a dictionary to group the data and push it into an array. The array is then ...

SQL's COUNT function in ASP.NET

Let's focus on the tables that are relevant to this issue: https://i.sstatic.net/hwmVv.png The information I need to display in a table includes: CategoryName | DateCreated | DateUpdate | No. of Products The challenge lies in the fourth column, N ...

Retrieve the `access_token` attribute from local storage

I have stored the token in local storage using: localStorage.setItem('token', JSON.stringify(res.data)). Now, I am attempting to retrieve the access_token property. JSON.parse(localStorage.getItem(token['access_token'])) An error is o ...

Acquiring a DataRowCollection

Is it possible to sort a DataRowCollection in ASP.NET WebForms like we do with a List in ASP.NET MVC? public DataRowCollection GetSortedRow(DataRowCollection dataRows) { var sortedResult = dataRows.OrderBy(d => d["Name"]) } Unfortunate ...

Tips for addressing flickering issues when scrolling on your device

I am facing an issue with two elements that are set to a fixed position on the page. When these elements reach the bottom of the page, I want them to revert back to a static position using JavaScript. The problem occurs when trying to scroll by clicking a ...

Exploring the usage of the Date method in React

When I insert the given code snippet import React,{Component} from 'react'; import ReactDOM from 'react-dom'; import './index.css'; class Date extends React.Component{ dataToString = (d) =>{ ...

Uploading files with node.js and express

Currently, I am following the steps outlined in this tutorial: I have successfully followed the instructions until the section where they set up the routes without actually creating any views. The tutorial states: That's it, we have completed sett ...

Is it a good idea to relocate the document.ready function into its own JavaScript function?

Can the following code be placed inside a separate JavaScript function within a jQuery document.ready, allowing it to be called like any other JavaScript function? <script type="text/javascript"> $(document).ready(function() { $('div#infoi ...

What is the process for modifying JSON attributes with JavaScript?

One JSON data set is provided below: [{ ID: '0001591', name: 'EDUARDO DE BARROS THOMÉ', class: 'EM1A', 'phone Pai': '(11) 999822922', 'email Pai': '<a href="/cdn-cgi/l/em ...

The jsonGenerator is unable to process strings containing spaces

Hey, I'm having trouble passing a string with whitespaces to my JavaScript function using jsonGenerator. Here's the code snippet: jGenerator.writeStringField(cols[8], ap.getContentId() != null ? "<img src='img/active.png' onclick=au ...

The directive in an Angular app is not loaded due to lazy loading

Seeking assistance with lazy loading a directive in my Angular application, I am using ui.router and oc.lazyLoad. Below is the code snippet: menu : <ul> <li><a ui-sref="home">Home</a></li> <li><a ui-sre ...

Is there a method to avoid redeclaring variables in JavaScript with jQuery?

In the structure of my code, I have the following setup. <!-- first.tpl --> <script> $(document).ready(function() { objIns.loadNames = '{$names|json_encode}'; } ) </script> {include file="second.tpl"} <! ...

Enhance ng-grid with row expansion and collapse feature

I am looking to create a feature in ng-grid that allows for expanding and collapsing rows, similar to what is demonstrated here http://datatables.net/examples/api/row_details.html. If you click the "plus icon," more details are revealed. I found a related ...

Using PHP, JavaScript, and Bootstrap to display success or error messages for form fields through AJAX

I am working on a form where users input an 'authorisation code' that is then checked against a database using AJAX and PHP. Currently, the form displays a tick if the code is valid and a cross if it is incorrect. I would like to utilize Bootstra ...

Struggling with AngularJS routing woes

I've been struggling to get my JavaScript code working. As a JavaScript newbie, I was trying to implement a routing example in AngularJS that I found online. Despite spending numerous hours on it, I couldn't get it to work. Problem: The default ...