File upload control not working inside Update panel even after postback trigger

StackOverflow https://stackoverflow.com/questions/23487818

  •  16-07-2023
  •  | 
  •  

سؤال

i am facing strange issue: File upload is not working inside update panel. I know this is common issue, however common solutions are not working here. i.e. I have tried using post-back trigger for upload button.

Code is like:

<asp:UpdatePanel runat="server">
                <ContentTemplate>
                    <div runat="server" id="divAttachments" style="margin-top: 10px;">
                        <div class="attachment collapsableDiv" onclick="HideShowAttachments();" style="margin-left: auto;
                            margin-right: auto;">
                            <img id="imgStatusMain" src="../Images/open.gif" class="showHide rightImage" runat="server" />&nbsp;
                            Attachments <span class="mandatory">*</span>
                        </div>
                        <div id="attachmentDiv" style="margin-left: auto; margin-right: auto;">
                            <asp:GridView runat="server" ID="gvAttachments" CssClass="innerGrid" EmptyDataText="No attachments added"
                                DataKeyNames="Id" AutoGenerateColumns="false" OnRowDataBound="gvAttachments_RowDataBound"
                                OnRowCommand="gvAttachments_RowCommand">
                                <Columns>
                                    <asp:TemplateField HeaderText="File name">
                                        <ItemTemplate>
                                         <asp:LinkButton
                                                runat="server" ID="gvlblFileName" Text='<%#Eval("FileName") %>' OnClick="gvlblFileName_Click"></asp:LinkButton>
                                            <asp:HiddenField runat="server" ID="hfFileName" Value='<%#Eval("Id") %>' />
                                        </ItemTemplate>
                                    </asp:TemplateField>
                                    <asp:TemplateField HeaderText="Action">
                                        <ItemTemplate>
                                            <asp:LinkButton ID="lbtnDelete" runat="server" CausesValidation="False" CommandName="remove"
                                                Text="Delete" ToolTip="Click to delete" CommandArgument='<%# DataBinder.Eval(Container, "RowIndex") %>'></asp:LinkButton>
                                        </ItemTemplate>
                                        <ItemStyle CssClass="action" />
                                    </asp:TemplateField>
                                </Columns>
                            </asp:GridView>
                            <span class="label" runat="server" id="spnAttachments" style="margin: 10px 10px 10px 50px;">
                                Attachments</span>
                            <asp:FileUpload runat="server" ID="fuAttachements" onchange="UploadFile();" Style="margin: 10px 0px 10px 0px;" /><asp:LinkButton
                                runat="server" ID="lbUploadFile" OnClick="lbUploadFile_Click"></asp:LinkButton>
                        </div>
                    </div>
                </ContentTemplate>
                <Triggers>
                    <asp:AsyncPostBackTrigger ControlID="ddlCategory" EventName="SelectedIndexChanged" /> 
                    <asp:PostBackTrigger ControlID="lbUploadFile" />
                </Triggers>
            </asp:UpdatePanel>

And my cs code is like:

if (string.IsNullOrEmpty(fuAttachements.FileName))
        result.AddError("Please give valid file name.");
    else
    { //code goes here}

I always get blank file name

Is there any other solution for this?. I can not remove update panel.

I have searched on Google and have checked lots of links, but everywhere same solution is given

هل كانت مفيدة؟

المحلول

unfortunately the fileupload does not work in an update panel; it never has. it requires a full 'normal' postback. there's no way around it. you have to move it out of the panel.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top