【问题标题】:VB ASP.NET 4.0 - Adding Example to new web projectVB ASP.NET 4.0 - 将示例添加到新的 Web 项目
【发布时间】:2014-08-27 15:34:35
【问题描述】:

我正在尝试学习 ASP.NET(经过多年使用经典的 ASP、jQuery、Ajax 等)。我已经安装了 VS 2010,并且在我的 W7 64 位 PC 上运行了 IIS。

我创建了一个名为 ASP.NET-4.0 的新 Web 项目(在名为 C:\ASP.NET Testing\ASP.NET 4.0 示例的文件夹中)。

我可以很好地编译项目并运行默认页面等。

我正在尝试从书中加载一个简单的示例(ASP.NET 4.0 In Practice)。所以,我创建了一个子文件夹 CH01,然后复制到 .aspx 和 .aspx.vb 文件中。

当我调试这个时,我得到了

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

Parser Error Message: Could not load type 'ASP.NET_4._0.Global_asax'.

Source Error: 

 Line 1:  <%@ Application Codebehind="Global.asax.vb" Inherits="ASP.NET_4._0.Global_asax" Language="vb" %>

在浏览器窗口中。

示例代码(已从网站下载)是:

<%@ Page Language="VB" AutoEventWireup="false" CodeFile="1-4.aspx.vb" Inherits="_1_4" %>
<html>
<head>
<title>Listing 1.4</title>
</head>
<body>
    <form id="Form1" runat="server">
        <div>
            <asp:literal id="ResponseText" runat="server" />
            <br />
            Enter your name:
            <asp:textbox runat="server" ID="Name" />
            <br />
            <asp:button runat="server" Text="Click Me" ID="ClickButton" OnClick="HandleSubmit" />
        </div>
    </form>
</body>
</html>

Partial Class _1_4
    Inherits System.Web.UI.Page

    Sub HandleSubmit(ByVal sender As Object, ByVal e As EventArgs)
        ResponseText.Text = "Your name is: " & Name.Text
    End Sub

End Class

在 VS 中,我还收到一条错误消息,突出显示 ResponseText.Text = "Your name is: " & Name.Text

'ResponseText' is not declared. It may be inaccessible due to its protection level. 

Global.asxa 文件

Imports System.Web.SessionState

Public Class Global_asax
    Inherits System.Web.HttpApplication

    Sub Application_Start(ByVal sender As Object, ByVal e As EventArgs)
        ' Fires when the application is started
    End Sub

    Sub Session_Start(ByVal sender As Object, ByVal e As EventArgs)
        ' Fires when the session is started
    End Sub

    Sub Application_BeginRequest(ByVal sender As Object, ByVal e As EventArgs)
        ' Fires at the beginning of each request
    End Sub

    Sub Application_AuthenticateRequest(ByVal sender As Object, ByVal e As EventArgs)
        ' Fires upon attempting to authenticate the use
    End Sub

    Sub Application_Error(ByVal sender As Object, ByVal e As EventArgs)
        ' Fires when an error occurs
    End Sub

    Sub Session_End(ByVal sender As Object, ByVal e As EventArgs)
        ' Fires when the session ends
    End Sub

    Sub Application_End(ByVal sender As Object, ByVal e As EventArgs)
        ' Fires when the application ends
    End Sub

End Class

我显然错过了一些非常简单的东西,但我不明白。我看不到任何关于我需要在 VS 中设置的任何说明。

谢谢。

如果我从 VS 添加,此页面可以正常工作。实际上与我从示例中复制的页面相同,但页面标记略有变化。它还有一个由VS自动生成的.designer.vb页面。

14.aspx
<%@ Page Language="vb" AutoEventWireup="false" CodeBehind="14.aspx.vb" Inherits="ASP.NET_4._0._14" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html>
<head>
<title>Listing 1.4</title>
</head>
<body>
    <form id="Form1" runat="server">
        <div>
            <asp:literal id="ResponseText" runat="server" />
            <br />
            Enter your name:
            <asp:textbox runat="server" ID="Name" />
            <br />
            <asp:button runat="server" Text="Click Me" ID="ClickButton" OnClick="HandleSubmit" />
        </div>
    </form>
</body>
</html>

14.aspx.vb
Public Class _14                                                                                
    Inherits System.Web.UI.Page                                                                 

    Sub HandleSubmit(ByVal sender As Object, ByVal e As EventArgs)                              
        ResponseText.Text = "Your name is: " & Name.Text                                        
    End Sub                                                                                     

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

    End Sub                                                                                     

End Class

14.aspx.designer.vb
'------------------------------------------------------------------------------                                                                                         
' <auto-generated>                                                               
'     This code was generated by a tool.                                         
'                                                                                
'     Changes to this file may cause incorrect behavior and will be lost if      
'     the code is regenerated.                                                   
' </auto-generated>                                                              
'------------------------------------------------------------------------------  

Option Strict On                                                                 
Option Explicit On                                                               


Partial Public Class _14                                                         

    '''<summary>                                                                 
    '''Form1 control.                                                            
    '''</summary>                                                                
    '''<remarks>                                                                 
    '''Auto-generated field.                                                     
    '''To modify move field declaration from designer file to code-behind file.  
    '''</remarks>                                                                
    Protected WithEvents Form1 As Global.System.Web.UI.HtmlControls.HtmlForm     

    '''<summary>                                                                 
    '''ResponseText control.                                                     
    '''</summary>                                                                
    '''<remarks>                                                                 
    '''Auto-generated field.                                                     
    '''To modify move field declaration from designer file to code-behind file.  
    '''</remarks>                                                                
    Protected WithEvents ResponseText As Global.System.Web.UI.WebControls.Literal

    '''<summary>                                                                 
    '''Name control.                                                             
    '''</summary>                                                                
    '''<remarks>                                                                 
    '''Auto-generated field.                                                     
    '''To modify move field declaration from designer file to code-behind file.  
    '''</remarks>                                                                
    Protected WithEvents Name As Global.System.Web.UI.WebControls.TextBox        

    '''<summary>                                                                 
    '''ClickButton control.                                                      
    '''</summary>                                                                
    '''<remarks>                                                                 
    '''Auto-generated field.                                                     
    '''To modify move field declaration from designer file to code-behind file.  
    '''</remarks>                                                                
    Protected WithEvents ClickButton As Global.System.Web.UI.WebControls.Button  
End Class                                                                        

【问题讨论】:

    标签: asp.net visual-studio-2010


    【解决方案1】:

    在您网站的根目录上检查您的global.asax。类名/命名空间似乎有问题。我认为这与您添加的文件无关,而是与您可能错误地移动或删除的文件有关。

    【讨论】:

    • 我没有接触过 global.asax 文件。如果我删除添加的文件,一切正常。我想知道这是否与继承声明有关?
    • 看看吧。 .net 找不到类名。 global.asax 中的类名是什么?
    • 我已将 global.asxa 内容添加到帖子中。
    • 你可以看到类名是Global_asax而不是ASP.NET_4._0.Global_asax。所以,正如我所说,命名空间存在问题。将类名改为ASP.NET_4._0.Global_asax
    • 我已将 global.asax 文件更改为 Public Class ASP.NET_4._0.Global_asax 。但是,VS 现在在 ASP 后面的字符下划线,并且错误说 End Of Statement Expected。项目名称有限制吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-07-19
    • 1970-01-01
    • 2015-10-28
    • 1970-01-01
    • 2017-11-29
    • 2023-03-05
    • 1970-01-01
    相关资源
    最近更新 更多