asp.net 如何添加内联VB.NET方法?

x6h2sr28  于 5个月前  发布在  .NET
关注(0)|答案(3)|浏览(52)

由于我无法将.vb代码隐藏文件添加到我的.aspx页面中,如划定的here,我试图像这样添加方法“inline”(“ConvertSubcategoryIndex”函数是新的代码位):

<%
        . . .  
    If Request.Form.Item("Action") = "Save" Then
        . . .
                        .Parameters.Add("@Subcategory", SqlDbType.NVarChar).Value = ConvertSubcategoryIndex(Category, Subcategory)
        . . .
    End If 'If Save Action

    Function ConvertSubcategoryIndex(_Category As String, _Subcategory As Int32) As String
        If _Category = "New"
            If _Subcategory = 0
                Return "New"
            End If
            Else If _Subcategory = 1
                Return "Assumed"
            End If    
        End If
        If _Category = "Existing"
            If _Subcategory = 0
                Return "Existing"
            End If
            Else If _Subcategory = 1
                Return "Organic"
            End If    
        End If
        Return "Unknown"
    End Function
%>

字符串
但这行不通,我得到了这个:

Compilation Error 

Description: An error occurred during the compilation of a resource required to service this request. Please review the following specific error details and modify your source code appropriately. 

Compiler Error Message: BC30289: Statement cannot appear within a method body. End of method assumed.

Source Error:

Line 274:    End If 'If Save Action
Line 275:    
Line 276:    Function ConvertSubcategoryIndex(_Category As String, _Subcategory As Int32) As String
Line 277:        If _Category = "New"
Line 278:            If _Subcategory = 0

Source File: C:\Users\cshannon\Source\Workspaces\CSReports\EnhancedMonthlySalesReporting\customerreportingnet\customerreportingnet\pages\custmaint_entry.aspx    Line: 276

**注意:**无论我将函数放在上面还是下面,我都会得到这个 *End If 'If保存操作 *

更新

基于某人的别名(Noise),我尝试在顶部添加一个部分,代码如下:

. . .
</head>

    <%
    Function ConvertSubcategoryIndex(_Category As String, _Subcategory As Int32) As String
        If _Category = "New"
            If _Subcategory = 0
                Return "New"
            End If
            Else If _Subcategory = 1
                Return "Assumed"
            End If    
        End If
        If _Category = "Existing"
            If _Subcategory = 0
                Return "Existing"
            End If
            Else If _Subcategory = 1
                Return "Organic"
            End If    
        End If
        Return "Unknown"
        End Function    
        %>

<%
    Unit = Request.QueryString.Item("Unit")
. . .


.但这没有什么区别;我仍然得到,“语句不能出现在方法体中。假定方法结束。”

更新2

即使使用tgolisch的改进代码:

Function ConvertSubcategoryIndex(_Category As String, _Subcategory As Int32) As String
    If _Category = "New" Then  'really need a "Then" for every "If"
        If _Subcategory = 0 Then
            Return "New"
        'End If  'this End If was killing the next line
        ElseIf _Subcategory = 1 Then
            Return "Assumed"
        End If
    End If
    If _Category = "Existing" Then
        If _Subcategory = 0 Then
            Return "Existing"
        'End If  'this one was causing problems too
        ElseIf _Subcategory = 1 Then   'ElseIf can't have a space between Else If
            Return "Organic"
        End If
    End If
    Return "Unknown"
End Function


.我得到了和以前一样的err msg:

Compilation Error 
Description: An error occurred during the compilation of a resource required to service this request. Please review the following specific error details and modify your source code appropriately. 

Compiler Error Message: BC30289: Statement cannot appear within a method body. End of method assumed.

Source Error:

Line 63:     <%
Line 64:     'End Sub
Line 65:     Function ConvertSubcategoryIndex(_Category As String, _Subcategory As Int32) As String
Line 66:     If _Category = "New" Then  'really need a "Then" for every "If"
Line 67:         If _Subcategory = 0 Then

Source File: C:\Users\cshannon\Source\Workspaces\CSReports\EnhancedMonthlySalesReporting\customerreportingnet\customerreportingnet\pages\custmaint_entry.aspx    Line: 65


更奇怪的是,Visual Studio发现代码的第一行有问题,并提出了一个奇怪的建议,即我在那里添加一个“End Sub”:
x1c 0d1x的数据
我试过了,只是为了好玩,但正如人们所怀疑的那样,这在其他地方也犯了错误。

更新3

基于这两个答案(tgolisch修复了代码本身,Sam Axe展示了如何将其添加到混合中),它现在可以工作了。通过这些修复和对我的逻辑的修复,它的工作状态是:

<script runat="Server">
    Function ConvertSubcategoryIndex(_Category As String, _Subcategory As String) As String
        If _Category = "New Business" Then
            If _Subcategory = "0" Then
                Return "New"
            ElseIf _Subcategory = "1" Then
                Return "Assumed"
            End If
        End If
        If _Category = "Existing Business" Then
            If _Subcategory = "0" Then
                Return "Existing"
            ElseIf _Subcategory = "1" Then
                Return "Organic"
            End If
        End If
        Return "Unknown"
    End Function
</script>

az31mfrm

az31mfrm1#

我已经很久没有做过WebForms了......但是如果我没记错的话,你需要使用这样的东西:

<script runat="Server">
    Public Function Whatever() As String
        Return "something"
    End Function
</script>

字符串

nbysray5

nbysray52#

您的VB.NET语法有几个语法错误。请尝试为您的ConvertSubcategoryIndex()函数粘贴以下代码:

<%
Function ConvertSubcategoryIndex(_Category As String, _Subcategory As Int32) As String
    If _Category = "New" Then  'really need a "Then" for every "If"
        If _Subcategory = 0 Then
            Return "New"
        'End If  'this End If was killing the next line
        ElseIf _Subcategory = 1 Then
            Return "Assumed"
        End If
    End If
    If _Category = "Existing" Then
        If _Subcategory = 0 Then
            Return "Existing"
        'End If  'this one was causing problems too
        ElseIf _Subcategory = 1 Then   'ElseIf can't have a space between Else If
            Return "Organic"
        End If
    End If
    Return "Unknown"
End Function    
%>

字符串
你可能还需要看看第一个例子顶部的代码质量,我不知道你把...放在哪里。

qltillow

qltillow3#

我现在有同样的问题,当我想显示日期值(从UNIX时间戳)与前导零纯VBScript.我定义了我的函数做一些格式化,但也收到BC30289编译器消息.然后我红了完整的编译器输出,喝了一杯咖啡,和.

Dim ts, td, t
    ts = "1690008848"
    td = DateAdd("s", ts, "01/01/1970 00:00:00")
    t = DatePart("yyyy", td) & "-"
    t = t & DatePartFormatter(td, "m") & "-"
    t = t & DatePartFormatter(td, "d") & " "
    t = t & DatePartFormatter(td, "h") & ":"
    t = t & DatePartFormatter(td, "n") & ":"
    t = t & DatePartFormatter(td, "s")
    Response.Write(resp & ", " & t)
    
    
    ' Here comes a small trick, after examining the full compiler output
    ' ASP pages run inside a Sub started by the system; in my case, this is called as
    '  Private Sub __Render__control1(ByVal __w As System.Web.UI.HtmlTextWriter, ByVal parameterContainer As System.Web.UI.Control)
    ' Everything in your ASP page code is inside this system Sub, that is why you get the error message of
    '  "Compiler Error Message: BC30289: Statement cannot appear within a method body. End of method assumed."
    ' when you try to define your own Subs or Functions to make your life easier.
    ' Fortunately, VB compiler is a good partner for a trick, because it only needs an exact string at a good position.
    ' That means: we simply have to close the system-opened Sub at the end of the page code, then we can write our Subs or Functions there,
    ' and finally we open an empty Sub, so that the system can close it instead of the original one, without reclaiming about anything.
    ' And, that's it :-)
    
    ' here we close the __Render_control1 system Sub...
    End Sub
    
    ' ...declare our program pieces, ...
    Function DatePartFormatter(d as Date, part as String)
        Dim var
        var = CStr(DatePart(part, d))
        If Len(var) = 1 Then
            var = "0" & var
        End If
        DatePartFormatter = var
    End Function
    
    ' ... and finally open a Sub, but let the system close it, after reading in the page code (by the compiler) ends completely
    Sub CompletelyDummyThing
    %>

字符串
这是一个有点丑陋,但它的工作。如果没有,检查你的ASP代码的父在编译器输出,并对齐。

相关问题