首页 > 解决方案 > FileUpload.HasFile 错误地返回 false

问题描述

ASP 控件FileUpload总是false为方法返回FileUpload.HasFile。我曾尝试使用触发器,但这对我不起作用。

这是按钮方法

protected void btnUploadFile_Clicked(object sender, EventArgs e)
{
  testMethod();
}

这是testMethod()if 语句总是评估为假。

protected void testMethod()
{
  if(FileUploadImage.HasFile)
  {
    ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "NoDatabaseAlertMessage", "alert('it work')", true);
  }
  else
  {
    ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "NoDatabaseAlertMessage", "alert('no work')", true);
  }
}

Edit1:这是我尝试实现的 html 触发器内容,但未成功

<div>
  <asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager>

  <asp:UpdatePanel ID="UpdatePanel1" UpdateMode="Always" runat="server">
    <Triggers>
      <asp:PostBackTrigger ControlID="btnUploadFile" />
    </Triggers>
  </asp:UpdatePanel>

  <asp:FileUpload ID="FileUploadImage" runat="server" />

  <asp:Button ID="btnUploadFile" runat="server" Text="Upload File" class="btn btn-primary transition-3d-hover" OnClick="btnUploadFile_Clicked" />

</div>

标签: c#htmlcssasp.net

解决方案


我注意到我团队中的某个人对 .aspx 文件中的代码进行了错误的编辑,并且没有关闭表单标记。在我关闭表单标签后,问题消失了,控件按预期运行。


推荐阅读