C# FileUpload.HasFile 总是错误
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13138247/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
FileUpload.HasFile give always false
提问by Neeraj Kumar Gupta
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
这是我的代码,其中我的 FileUpload 控件在更新面板之外,但是当我单击更新面板下的保存按钮时,给 fileUploadAttachment.HasFile = false
ASPX
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
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
请帮助我如何解决它
采纳答案by Grant Thomas
You'll need to add postback triggers for controls that post within the UpdatePanel:
您需要为在以下内容中发布的控件添加回发触发器UpdatePanel:
<asp:UpdatePanel ...>
<Triggers>
<asp:PostBackTrigger ControlID="btnSend" />
</Triggers>
...
</asp:UpdatePanel>
回答by Vikas
You Can Change Your Code in the ASP Page Like This
您可以像这样更改 ASP 页面中的代码
<asp:updatePanel>
<trigger>
<asp:PostBackTrigger ID="btnSend">
</trigger>
<\asp:updatePanel>

