0

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" />&nbsp;<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 中如何知道标签标签中是否有任何内容?

4

1 回答 1

0

我通过将 Visible 属性更改为 FALSE 来解决此问题:

<script runat="server">
    Protected 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("FULL Upload File Path\" & _
                       FileUpload1.FileName)
                    Label1.Text = "<div class='mission_gothic_reg success'>Statement Successfully Received, Thank You!<br />File name: " & _
                       FileUpload1.PostedFile.FileName & "<br>" & _
                       "File Size: " & _
                       FileUpload1.PostedFile.ContentLength & " kb</div>"
                       Button1.Visible = FALSE
                       FileUpload1.Visible = FALSE
                Catch ex As Exception
                    Label1.Text = "<div class='mission_gothic_reg fail'>ERROR: " & ex.Message.ToString() & "</div>"
                End Try
            Else
                Label1.Text = "<div class='mission_gothic_reg fail'>Only Word Document files allowed (.doc, .docx).</div>"
            End If
        Else
            Label1.Text = "<div class='mission_gothic_reg fail'>You have not specified a file.</div>"
        End If
    End Sub
</script>

也许这会帮助别人。

于 2013-07-18T04:48:14.140 回答