2013-04-28 33 views
1

我使用FormView控件edittemplate內FileUpload控件asp.net文件上傳總是返回false(沒有的UpdatePanel)

<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> 

這是working.But我添加一個UpdatePanel那麼它沒有工作,然後我但它仍然返回假(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 
      } 
      } 
     } 

總是去其他範圍。

回答

3

在使用中觸發才達到該

<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> 

代碼背後

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

你是否對fileupload控件的屬性做了任何事情,例如將autopost的back值設置爲false?嘗試設置爲true,如果它是假的。

+0

此外,爲什麼你的updatepanel不工作? – Callum 2013-04-28 14:42:26

+0

嘿@callum thganks但fileupload控制不具有autopostback屬性。我沒有做任何改變fileupload控制的屬性。我不相信,它工作。我remowed updatepanel因爲od this.But我認爲它沒有關於它 – serdar 2013-04-28 14:46:08

+0

嗯奇怪......這似乎是一個愚蠢的答案,但嘗試關閉並重新開放視覺工作室,這一直是我過去的問題。 – Callum 2013-04-28 15:34:14

0

我碰到這個問題來了,當我有這個問題,想我會發布什麼我的問題和解決方案都以及。

確保您嘗試上傳的文件大於0字節。我試圖上傳一些空白文本文件進行測試,每個文件的FileName屬性設置正確,但HasFile始終爲false。向文件中添加一些文本爲其提供了一些內容,並且文件能夠成功上傳。