[Solved] First Time ASP.Net FileUpload HasFile is false inside UpdatePanel

Sumeet
 
on Dec 23, 2020 04:01 AM
1633 Views

On Page_Load I have kept Panel visible=false. Panel has Fileupload and button. There are 2 options to visible Yes and No. If upload panel is visible then on first upload, Fileupload.HasFile gives false then from Second upload onwards Fileupload.HasFile gives true.

Why?

 aspx page: 

<%@ Page Title="" Language="C#" MasterPageFile="~/admin.Master" AutoEventWireup="true" CodeBehind="admin.aspx.cs" Inherits="schooleducation.admin1" %>
<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
    <asp:ScriptManager ID="ScriptManager1" runat="server" EnablePartialRendering="true" EnablePageMethods="true">
        </asp:ScriptManager>         
        <asp:UpdatePanel ID="UpdatePanel1" runat="server">             
            <ContentTemplate>
                <div class="login-register-area pt-10 pb-130">
                    <div class="container">
                        <div class="row">
                            <div class="col-lg-7 col-md-12 ml-auto mr-auto">
                                <div class="login-register-wrapper">
                                    <div class="login-register-tab-list nav">
                                        <a class="active" data-toggle="tab" href="#lg1">
                                            <h4>Upload </h4>
                                        </a>
                                    </div>
                                    <div class="tab-content logindesign">
                                        <div id="lg1" class="tab-pane active">
                                            <div class="login-form-container">
                                                <div class="login-register-form">
                                                    <div class="row">
                                                        <div class="col-lg-12 p-2">
                                                            <asp:RadioButtonList ID="RadioButtonList1" runat="server" AutoPostBack="true" OnSelectedIndexChanged="RadioButtonList1_SelectedIndexChanged"><asp:ListItem Value="1">Yes</asp:ListItem><asp:ListItem Value="0">No</asp:ListItem></asp:RadioButtonList>
                                                        </div>
                                                    </div>
                                                    <div class="row" id="upload1" runat="server" visible="false">
                                                        <div class="col-lg-12">
                                                    <asp:FileUpload ID="FileUpload1" CssClass="form-control" runat="server" />
                                                     <asp:RequiredFieldValidator ID="RequiredFieldValidator5" runat="server" ErrorMessage="*Browse file to upload" SetFocusOnError="true" Display="Dynamic" ControlToValidate="FileUpload1" ValidationGroup="" ForeColor="Red" Font-Italic="true" Font-Size="11px" EnableClientScript="true"></asp:RequiredFieldValidator>
                                                     <asp:RegularExpressionValidator ValidationGroup="tabdocs" CssClass="errmsg" EnableClientScript="true" Display="Dynamic" SetFocusOnError="true" ID="RegularExpressionValidator14" ControlToValidate="FileUpload1" runat="server" ErrorMessage="Invalid File Format. Only .jpg" ValidationExpression="([a-zA-Z0-9\s_\\.\-:])+(.jpg|.JPG)$"></asp:RegularExpressionValidator>
                                                            </div>
                                                    <div class="row">
                                                        <div class="col-lg-12 p-2">
                                                            <center>
                                                        <asp:Button ID="btnUpload" runat="server" CssClass="btn btn-primary b1" Text="Upload" OnClick="btnUpload_Click" />
                                                                </center>
                                                        </div>
                                                    </div>
                                                        </div>
                                                </div>
                                            </div>
                                        </div>                                         
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </ContentTemplate>
            <Triggers>
                <asp:PostBackTrigger ControlID="btnUpload" />
            </Triggers>
        </asp:UpdatePanel>
</asp:Content>

c# : 

        protected void btnUpload_Click(object sender, EventArgs e)
        {
            if (FileUpload1.HasFile)
            {
                Response.Write("Yes");
            }
            else
            {
                Response.Write("No");
            }
        }

 

Download FREE API for Word, Excel and PDF in ASP.Net: Download
Sumeet
 
on Dec 23, 2020 10:48 PM

I did got working by adding multipart/form-data in Form tag or add dynamically as below (recommended for the pages attached with master page):

if (!Page.IsPostBack)
{
   Page.Form.Attributes.Add("enctype", "multipart/form-data");
}