内部Listview插入,编辑Itemtemplate和EmptyData模板中的AsyncFileUpload不起作用

时间:2015-09-02 09:37:44

标签: javascript asp.net listview asp.net-ajax

AsyncFileUpload在Listview Insert,Edit Itemtemplate和EmptyData Template中不起作用。

以上是我的客户端功能

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

.aspx文件中的标记

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

尝试上传文件调用客户端事件OnClientUploadError 无法理解为什么会出错。 在简单页面和内部更新面板上工作相同的文件上传,但在列表视图内,它给客户端错误。 通过以上链接问题没有得到确切的答案,请帮助我。

How to find Ajaxfileupload control inside Listview When in Editmode

AsyncFileUpload within EditItemTemplate of ListView

1 个答案:

答案 0 :(得分:2)

我可以在您分享的代码中看到两个问题:

  1. 该行代码:var UploadControls = $('#<%= FileUpload.ClientID %> :input')
  2. 您的标记中没有ClientIDMode属性,因此它是Inherit
  3. 这两个问题的起源相同:您在AsyncFileUploader内使用ListView,因此页面中会有多个FileUpload个ID元素。< / p>

    因此,首先必须将函数声明更改为:

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

    然后,要获得input,您将使用:

    var UploadControls = $(sender._inputFile);
    

    最后,要修复第二个问题,您需要将ClientIDMode属性更改为AutoID,以便AsyncFileUploader能够正确找到其引用的客户成员。< / p>