c# - FileUpload.HasFile 错误地返回 false

标签 c# html css asp.net

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

这里是按钮方法

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

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

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>

最佳答案

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

关于c# - FileUpload.HasFile 错误地返回 false,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58664852/

相关文章:

c# - HttpNotificationChannel Open() 抛出 InvalidOperationException ("Failed to open channel")

c# - 最快的二维数据空间索引,一旦构建就无需更新

c# - 根据 XmlEnumAttribute 名称值检索枚举值

jquery - 在 DIV 中插入 Html 标签

javascript - 单击事件更改网格布局

c# - 极端线程安全集合

jquery - 根据 div 中的元素数使用 css 设置 css 属性

css - 是否有 CSS 父级选择器?

html - 2 "style"内联 css img 标签?

JavaScript 作为行内 block 去间距的强大 hack