3

I have an UpdatePanel which has an upload control and a button to upload. The button is set as a trigger, but the event handler for the button fails to execute 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 without fail on the second and subsequent times, just not the first. Is there any reason for this?

5
  • Any luck resolving this issue? Commented May 12, 2010 at 12:53
  • Unfortunately not. Behavior seems intermittent, which is the worst type of bug. Commented May 14, 2010 at 13:04
  • can you add server side code? Commented May 26, 2010 at 12:21
  • 1
    I would strip this down to the bare bones. update panel and button and see if it fires, than start adding controls and code back one at a time to see what breaks. it. Commented May 26, 2010 at 15:36
  • It doesn't fire the first time, even with just the button. Commented May 28, 2010 at 16:18

4 Answers 4

3

need to add enctype="multipart/form-data" in the form tag of the page.

faced the same problem and the above work around made it upload the file in the first attempt. hope some one will find it useful.

Sign up to request clarification or add additional context in comments.

Comments

1

I dont know why this is happening. However you could attempt to add the event yourself by override the OnInit page cycle event in the aspx.cs file. Remember to remove the Click event from your HTML page.

protected override void OnInit(EventArgs e)
{

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

Alternatively the Trigger might also need to be positioned directly below the UpdatePanel Tag

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

Comments

1

Maybe it's doing an AJAX postback the first time? On your UpdatePanel, try adding the attribute ChildrenAsTriggers="false".

This will stop any controls in the UpdatePanel causing asynchronous postbacks, so for every control that you want to cause a postback, you'll have to add an AsyncPostBackTrigger in the UpdatePanel's Triggers section.

1 Comment

Not something I'd thought of but it had no effect.
0

According to anbalagan's answer and an answer from another question, you should change the enctype property of the form element to 'multipart/form-data'. As it is said in this question's best answer, 'multipart/form-data' should be used whenever you write client-side code and need to post files to the server, e.g. use FileUpload controls.

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.