【问题标题】:FileUpload.HasFile give always falseFileUpload.HasFile 总是假的
【发布时间】:2012-10-30 11:42:46
【问题描述】:

这是我的代码,其中我的 FileUpload 控件位于更新面板之外,但是当我单击更新面板下的保存按钮时,给出 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);
    }
}

请帮助我如何解决它

【问题讨论】:

  • fileUploadAttachment 是什么类型,您没有向我们提供足够的源代码来帮助您。此外,没有一行将文件实际上传到控件。 The FileUpload control does not automatically save a file to the server after the user selects the file to upload. You must explicitly provide a control or mechanism to allow the user to submit the specified file. For example, you can provide a button that the user clicks to upload the file. - 似乎你遗漏了一些代码,或者只是还没有编写。
  • 再次查看代码,您似乎尝试上传/保存文件,但从未将文件添加到控件本身。你可能想阅读这个简单的例子msdn.microsoft.com/en-us/library/…
  • 我正在附加一个普通文本文件并使用保存按钮单击 (btnSave_Click) 进行上传
  • 这也是stackoverflow.com/questions/1941099/…的可能副本

标签: c# asp.net


【解决方案1】:

您需要为UpdatePanel 中发布的控件添加回发触发器:

<asp:UpdatePanel ...>
  <Triggers>
    <asp:PostBackTrigger ControlID="btnSend" />
  </Triggers>
  ...
</asp:UpdatePanel>

【讨论】:

  • 好的,谢谢。仅供参考,不要像我一样傻,将FileUpload 的控件添加到&lt;asp:PostBackTrigger ControlID - 你想要其他导致回发的控件。
  • 此外,如果您在母版页中有 UpdatePanel,则无法在此处添加 PostBackTrigger。因此,在这种情况下,您需要通过代码 ScriptManager sm = ScriptManager.GetCurrent(this.Page); scriptManager.RegisterPostBackControl(this.btnUpload); //change btnUpload to the right Button's ID 执行此操作
【解决方案2】:

您可以像这样在 ASP 页面中更改代码

<asp:updatePanel>
<trigger>
<asp:PostBackTrigger ControlID="btnSend">
</trigger>
<\asp:updatePanel>

【讨论】:

  • PostBackTrigger 控件没有 ID 属性
猜你喜欢
  • 1970-01-01
  • 2015-03-28
  • 2019-10-11
  • 2020-10-13
  • 1970-01-01
  • 2011-06-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多