2014-08-27 47 views
0

我想學習ASP.NET(經過多年使用經典的ASP,jQuery,Ajax等)。我已經安裝了VS 2010,並且在我的W7 64位個人電腦上運行了IIS。VB ASP.NET 4.0 - 將示例添加到新的Web項目

我創建了一個名爲ASP.NET-4.0的新Web項目(位於名爲C:\ ASP.NET Testing \ ASP.NET 4.0示例的文件夾中)。

我可以編譯工程優良,運行默認頁等

我試圖從一本書(ASP.NET 4.0在實踐)在一個簡單的例子來加載。所以,我創建了一個子文件夾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 =「您名字是:「& 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                   

回答

0

在您的網站的根目錄上檢查global.asax。類名/名稱空間似乎有問題。我認爲這與您添加的文件沒有任何關係,而是與您可能錯誤移動或刪除的內容有關。

+0

我沒有碰過global.asax文件。如果我刪除添加的文件,一切工作正常。我想知道這是否與繼承聲明有關? – Keith 2014-08-27 19:36:09

+0

只是看看它。 .net無法找到類名稱。 global.asax中的類名是什麼? – Ted 2014-08-27 19:37:17

+0

我已將global.asxa內容添加到帖子中。 – Keith 2014-08-27 19:39:59