Search code examples
asp.netfile-uploadformview

asp.net file upload inside formview always return false (not in updatepanel)


I am using fileupload control inside formview edittemplate

    <asp:FileUpload ID="fileup_profilfoto" runat="server" />
      <asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server" ErrorMessage="Sadece şu formatlar (.jpg, .bmp, .png, .gif)" ValidationExpression="^.*\.(jpg|JPG|png|PNG|bmp|BMP|gif|GIF)$" ControlToValidate="fileup_profilfoto" ForeColor="#00C0CC"></asp:RegularExpressionValidator>

It was working.But I added an updatepanel then it didnt work,and then I remowed update panel.But it's still return false (hasfile)

  protected void frmviewProfil_ItemUpdating(object sender, FormViewUpdateEventArgs e)
    {
        try
        {
            FileUpload fileup_profilfoto = (FileUpload)frmviewProfil.FindControl("fileup_profilfoto");
            if (fileup_profilfoto.HasFile)
            {
                //do something
            }
           else
            {
            //do something
            }
          }
      }

always goes else scopes.


Solution

  • hi use triggers to achive that

     <asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="conditional">
      <Triggers>
        <asp:PostBackTrigger ControlID="Button1" />
     </Triggers>
     <ContentTemplate>
       <asp:Label ID="Label1" runat="server"></asp:Label><br /><br />
       <asp:FileUpload ID="FileUpload1" runat="server" />
       <asp:Button ID="Button1" runat="server" Text="Upload" OnClick="Button1_Click" />
      </ContentTemplate>
     </asp:UpdatePanel>
    

    code behind

      protected void Button1_Click(object sender, EventArgs e)
      {
          if (FileUpload1.HasFile)
          {
              Label1.Text = FileUpload1.FileName;
          }
      }