Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

FileUpload.HasFile give always false

Tags:

c#

asp.net

this is my code where my FileUpload control is outside of update panel but when I click on save button which is under update panel give fileUploadAttachment.HasFile = false

ASPX

<asp:Literal runat="server" ID="lblAttachment" Text="Attachment:" /><asp:FileUpload
            ID="fileUploadAttachment" runat="server" Width="488px" />
        <asp:UpdatePanel ID="updatePanelAction" runat="server" UpdateMode="Always">
            <ContentTemplate>
                <asp:Button ID="btnSave" runat="server" Text="Save" OnClick="btnSave_Click" ValidationGroup="Save" />
                <asp:Button ID="btnTest" runat="server" Text="Test" Enabled="false" OnClick="btnTest_Click" />
                <asp:Button ID="btnConfirmTest" runat="server" Text="Confirm Test" Enabled="false"
                    OnClick="btnConfirmTest_Click" />
                <asp:Button ID="btnSend" runat="server" Text="Send" Enabled="false" OnClick="btnSend_Click" />
            </ContentTemplate>

        </asp:UpdatePanel>

CS

protected void btnSave_Click(object sender, EventArgs e)
{
    CampaignBAL campaignBAL;
    string tmpFileName = "";
    User user;
    Campaign campaignDetail = new Campaign();
    int? campaignID;

    if (fileUploadAttachment.HasFile) // return always false
    {
        tmpFileName = string.Format("{0}\\{1}{2}", Server.MapPath("TempUpload"), Guid.NewGuid(), Path.GetExtension(fileUploadAttachment.PostedFile.FileName));
        fileUploadAttachment.PostedFile.SaveAs(tmpFileName);
    }
}

please help me how can I fix it

like image 254
Neeraj Kumar Gupta Avatar asked Oct 30 '12 11:10

Neeraj Kumar Gupta


People also ask

What is importance of FileUpload control HasFile property?

The HasFile property gets a value indicating whether the FileUpload control contains a file to upload. Use this property to verify that a file to upload exists before performing operations on the file.

Which property is associated with FileUpload control?

Use the FileName property to get the name of a file on a client to upload by using the FileUpload control. The file name that this property returns does not include the path of the file on the client. The FileContent property gets a Stream object that points to a file to upload.

How does ASP FileUpload work?

ASP.NET FileUpload control allows us to upload files to a Web Server or storage in a Web Form. The control is a part of ASP.NET controls and can be placed to a Web Form by simply dragging and dropping from Toolbox to a WebForm. The FileUpload control was introduced in ASP.NET 2.0.


2 Answers

You'll need to add postback triggers for controls that post within the UpdatePanel:

<asp:UpdatePanel ...>
  <Triggers>
    <asp:PostBackTrigger ControlID="btnSend" />
  </Triggers>
  ...
</asp:UpdatePanel>
like image 64
Grant Thomas Avatar answered Sep 20 '22 23:09

Grant Thomas


You Can Change Your Code in the ASP Page Like This

<asp:updatePanel>
<trigger>
<asp:PostBackTrigger ControlID="btnSend">
</trigger>
<\asp:updatePanel>
like image 24
Vikas Avatar answered Sep 18 '22 23:09

Vikas