我runat
在 .aspx 页面的公共子中使用命令,如下所示:
<% Dim hasLabel %>
<script runat="server">
Public Sub Button1_Click(ByVal sender As Object, _
ByVal e As System.EventArgs)
If FileUpload1.HasFile Then
Dim fileExt As String
fileExt = System.IO.Path.GetExtension(FileUpload1.FileName)
If (fileExt = ".docx" OR fileExt = ".doc") Then
Try
FileUpload1.SaveAs("path_to_file" & _
FileUpload1.FileName)
Label1.Text = "<div class='centertext'>Statement Successfully Received, Thank You!<br /><br />File name: " & _
FileUpload1.PostedFile.FileName & "<br>" & _
"File Size: " & _
FileUpload1.PostedFile.ContentLength & " kb</div>"
Catch ex As Exception
Label1.Text = "ERROR: " & ex.Message.ToString()
End Try
Else
Label1.Text = "Only Word Document files allowed (.doc, .docx)."
End If
Else
Label1.Text = "You have not specified a file."
End If
hasLabel = Label1.Text
End Sub
</script>
稍后在页面下方,我希望能够检查hasLabel
内容,如果它不为空,则不仅仅是在页面上输出,否则,再次输出表单,如下所示:
<body>
<form id="form1" runat="server">
<% If hasLabel <> "" Than %>
<div class="centertext"><%= hasLabel %></div>
<% Else %>
<div>
<asp:FileUpload ID="FileUpload1" runat="server" class="file" /><br />
<br />
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Upload File" /> <br />
<asp:Label ID="Label1" runat="server" class="mission_gothic_reg smalltext"></asp:Label>
</div>
<% End If %>
</form>
</body>
我怎样才能做到这一点?现在,我收到一条错误消息: Name 'hasLabel' is not declared
基本上,我想要发生的是......如果文件已经提交,则不要显示文件上传按钮。当然,我很可能需要更改此设置,以便仅在成功上传且文件类型正确时才执行此操作。但我什至找不到一个简单的解决方案来了解它是否已正确上传到实际的 ASP 页面中。是的,我可以输出操作标签的文本,但是在 ASP.NET 中如何知道标签标签中是否有任何内容?