ASP.NET-如何用VB从Sub中检索变量



我在.aspx页面的Public Sub中使用runat命令,如下所示:

<% 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中,如何知道标签标签中是否有任何内容?

我通过将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>

也许这会帮助其他人。

相关内容

  • 没有找到相关文章

最新更新