2010-09-11 105 views
3

出於某種原因,我在嘗試測試計算機上的代碼頁時出現此錯誤。ASP.NET無效元素名稱StartTag錯誤

它可能與錯誤的IIS安裝有關,但我似乎無法找出問題所在。

我得到以下錯誤:

error on line 1 at column 2: StartTag: invalid element name

這裏是我的Default.aspx:

<%@ Page Language="C#" %> 

<html> 

<head> 

<title>Plating Trees</title> 

<script runat=」server」> 
protected void Page_Load(Object Source, EventArgs E) 
{ 

/* Begin C# Code!*/ 

Tree tree1 = new Tree(); 

tree1.Grow(3); 

tree1.Message(); 

} 

</script> 

</head> 

<body> 

<p><asp:label runat=」server」 id=」Output」 /></p> 

</body> 

</html> 

Tree.cs:

/* A simple C# class! */ 

public class Tree 
{ 

    public int height = 0; 

    public void Grow(int h) 
    { 
     height += h; 
    } 

    public string Message() 
    { 
     Output.Text = "The height of tree1 is:<br/>」 + tree1.height + feet"; 
    } 

} 

回答

1

不知道這是你的問題的原因,但你的屬性和代碼的雙引號看起來不正確。

<script runat=」server」> 
<p><asp:label runat=」server」 id=」Output」 /></p> 

應該是:

<script runat="server"> 
<p><asp:label runat="server" id="Output" /></p> 

Output.Text = "The height of tree1 is:<br/>」 + tree1.height + feet"; 

應該是:

Output.Text = "The height of tree1 is:<br/>" + tree1.height + "feet"; 
+0

哎呦,不能相信我沒有意識到, , 謝謝! – Qcom 2010-09-11 05:59:14

1

你的問題是類似的StackOverflow這個前面的問題:「StartTag: invalid element name」 in default.aspx,這瑟ems就像IIS配置問題。

從鏈接:

When I went into the ASP.NET tab for the virtual directory I noticed the ASP.NET version was not selected (it was an empty combo box). Choosing the .NET framework version did the trick.

看一看它,它可能會解決你的問題:)

+0

謝謝,這也有幫助,但是,我仍然有問題! :)我正在努力。 – Qcom 2010-09-11 05:59:59

1

的主要問題是,你的Tree類不必Output訪問,因爲該屬性屬於不同的對象,Default.aspx。您需要撥打Output.TextDefault.aspx而不是Tree。另外,我認爲傑夫是正確的關於因此,編輯您Page_Load如下:

在你的Default.aspx

<script runat="server"> 
protected void Page_Load(Object Source, EventArgs E) 
{ 
    /* Begin C# Code!*/ 
    Tree tree1 = new Tree(); 
    tree1.Grow(3); 
    Output.Text = tree1.Message(); 
} 
</script> 

Message()你需要刪除Output.Text 。接下來,您不能將類Tree參考爲tree1.height,因此請將其更改爲this.height或最好只是height。同時,請刪除feet,因爲你還沒有定義任何地方。請注意,此方法中也有」。最後,拿出最後的」,它位於feet的右側。編輯如下:

在你Test

public string Message() 
{ 
    return "The height of tree1 is:<br/>" + height; 
} 

你可以把一切相同,但就個人而言,我會移動<script runat="server">塊略低於<%@ Page Language="C#" %>

另外,我認爲你的邏輯存在錯誤,但是我相信,一旦你通過這些東西,你就可以知道這一點。

(我跑從我的VS 2008 IDE)

+0

謝謝,你的意思是我的邏輯錯誤?你是說我設置超級簡單的「程序」的方式? – Qcom 2010-09-11 06:00:35

+0

@BOSS:我的意思是說,當我運行它時,輸出不是你想要的,因爲你的'Message()'方法仍然關閉。查看我的解決方案更新。 – JohnB 2010-09-11 15:30:15

+0

@BOSS,就設置此程序而言,我認爲大多數ASP.NET程序員更喜歡使用「代碼隱藏」頁面類型,因此您的方法位於名爲'Default.aspx.cs'的單獨文件中 – JohnB 2010-09-11 15:45:40

0

@BOSS,也許你需要運行ASPNET_REGIIS工具在IIS中重新註冊ASP.NET。

進入您的.NET框架命令提示符並運行「aspnet_regiis -i」來執行此操作。

參考:ASP.NET IIS Registration Tool (Aspnet_regiis.exe)

而且,請注意以下幾點,如果你在你的IIS多的網站:

The -i flag causes the aspnet_regiis command to perform its work on every website on the box, not just the one that needs it. As the .NET framework 2.0 begins to ship there will be more developers and production servers running both version of the framework. Running the aspnet_regiis command with the -i flag will associate all websites on the box with the framework from where the command was run (there is one version of the aspnet_regiis command for every installed version of the .NET framework). It is also useful to note that the -i flag will reset auto-generated values with immediate impact on forms based logins and viewstate checksums. If you run the command with the -i flag on a live production server you may very well interupt the applications of logged in users of other applications on the same box. The 「-i「 flavor of the command should never be run in a production environment unless there are no active users logged on, and all websites on the box happen to be on the same .NET framework version.

來源:Running "aspnet_regiis -i" Not Always The Best Choice