ASP.NET - FileUpload with PostBack trigger

I have an UpdatePanel that has a download control and a download button. The button is set as a trigger, but the event handler for the button does not start on the first PostBack.

My aspx code is:

<asp:UpdatePanel ID="updPages" runat="server" UpdateMode="Conditional">
    <ContentTemplate>
        <div class="tabs">
            <ul>
                <li><asp:LinkButton ID="lnkContentsPages" runat="server" OnClick="updateContents" CommandArgument="pages">Pages</asp:LinkButton></li>
        <%-- these tabs change the ActiveViewIndex to show a different UserControl --%>
                <li><asp:LinkButton ID="lnkContentsImages" runat="server" OnClick="updateContents" CommandArgument="images">Images</asp:LinkButton></li>
            </ul>
            <div class="tabbedContent">
                <asp:MultiView runat="server" ID="mltContentsInner" ActiveViewIndex="0">
                    <asp:View ID="viwContentsImages" runat="server">
                        // ajax usercontrol for a list of images - works fine with ajax
                        <fieldset>
                            <legend>Upload New</legend>
                            <div class="formRow">
                                <asp:Label ID="lblFile" runat="server" Text="Filename" AssociatedControlID="uplFile" />
                                <asp:FileUpload ID="uplFile" runat="server" />
                            </div>
                            <div class="formRow">
                                <asp:Label ID="lblImageDescription" runat="server" Text="Description" AssociatedControlID="txtImageDescription" />
                                <asp:TextBox runat="server" ID="txtImageDescription" />
                            </div>
                            <asp:Button ID="btnUpload" runat="server" Text="Upload" CssClass="c3button btnUpload" CausesValidation="false" OnClick="btnUpload_Click" />
                        </fieldset>
                    </asp:View>
                    <asp:View ID="viwContentsPages" runat="server">
                        // ajax usercontrol - works fine with ajax
                    </asp:View>
                </asp:MultiView>
            </div>
        </div>
    </ContentTemplate>
    <Triggers>
        <asp:PostBackTrigger ControlID="btnUpload" />
    </Triggers>
</asp:UpdatePanel>

The button works flawlessly the second and subsequent times, and not in the first place. Is there a reason for this?

+3
source share
4 answers

you must add enctype = "multipart / form-data" in the page form tag.

ran into the same problem, and the work described above made him download the file on the first try. hope someone finds this useful.

+3
source

, . , OnInit aspx.cs. Click HTML-.

protected override void OnInit(EventArgs e)
{

btnUpload.Click+= new EventHandler(btnUpload_Click);
base.OnInit(e);
}

Trigger UpdatePanel

<asp:UpdatePanel ID="updPages" runat="server" UpdateMode="Conditional">  
 <Triggers> 
        <asp:PostBackTrigger ControlID="btnUpload" /> 
    </Triggers> 
+1

, AJAX ? UpdatePanel ChildrenAsTriggers="false".

UpdatePanel, , , , AsyncPostBackTrigger UpdatePanel Triggers.

+1

According to anbalagan's answer and the answer from another question, you should change the enctype property of the form element to "multipart / form-data". As the best answer to this question says , "multipart / form-data" should be used whenever you write code on the client side and must send files to the server, for example, use FileUpload controls.

I had the same problem as you, the form enctype has changed, and now everything works fine.

0
source

Source: https://habr.com/ru/post/1744494/


All Articles