C# – FileUpload not working in Multiview and updatepanel

ajaxasp.netcmultiviewupdatepanel

<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<asp:UpdatePanel runat="server">
<Triggers>
<asp:PostBackTrigger ControlID="Button1" />
</Triggers>
    <ContentTemplate>

<asp:MultiView ID="MultiView1" runat="server" 
                 ActiveViewIndex="0">           
    <asp:View ID="View1" runat="server">
        <asp:Button Text="next"
            runat="server" onclick="Unnamed1_Click" />
    </asp:View>
    <asp:View ID="View2" runat="server">
    <asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Button" 
        style="width: 56px" />
    <asp:FileUpload ID="fileupload2" runat="server" />
    </asp:View>
</asp:MultiView>
 </ContentTemplate>
</asp:UpdatePanel>

and the code behind is

 protected void Button1_Click(object sender, EventArgs e)
    {
        if (fileupload2.HasFile)
        {
            //code..
        }
    }
    protected void Unnamed1_Click(object sender, EventArgs e)
    {
        MultiView1.ActiveViewIndex = 1;
    }

When i click Button1 for uploading the image on first time the fileupload2.HasFile returns "false"

without refrshing the page again try to upload the same picture then fileupload2.HasFile returns "true"

Working Properly in View1. Problem is with View 2

What is the problem with that fileupload ?

Best Answer

Try registering the button control with RegisterPostBackControl early in the page lifecycle:

   protected void Page_Init(object sender, EventArgs e)
   {
       ScriptManager1.RegisterPostBackControl(Button1);
   }

See Also:

FileUpload and UpdatePanel: ScriptManager.RegisterPostBackControl works the second time.

Related Topic