2011-09-15 788 views
6

我想在aspx頁面中的服務器控件的代碼隱藏中調用MyMethod,如下所示。ASP.NET 4中'(單引號)和「(雙引號)之間的區別4

MyPage.aspx

<asp:Label ID="MyLabel" runat="server" Text='<%# MyMethod(Eval("MyColumn")) %>'> 

MyPage.aspx.cs

protected void MyMethod(object obj) { ... } 

如果我在aspx頁面使用"代替'然後它會給我一個編譯錯誤服務器標籤不是很好形成。如下。

<asp:Label ID="MyLabel" runat="server" Text='<%# MyMethod(Eval("MyColumn")) %>'> // This line work 
<asp:Label ID="MyLabel" runat="server" Text="<%# MyMethod(Eval("MyColumn")) %>"> // This line error 

我想知道爲什麼我需要使用單引號,這是一個規則嗎?如何在我的情況下使用雙引號?

回答

6

我想知道爲什麼我需要使用單引號,這是一個規則嗎?我如何在我的情況下使用雙引號 ?

單引號超過雙引號的用法只是爲了清楚字符串結尾的位置。您不能使用Text =「MyMethod(」123「)」「,因爲文本以M開始並可能以(或3或最後一個)結尾。通過使用單引號和雙引號,編譯器知道字符串何時結束。

Text="MyMethod('123')" 

Text='MyMethod("123")' 

你舉的例子是關於綁定,但讓說你想有一個雙引號,而使用雙引號的不具約束力的情況。您可以使用HTML實體"

Text="This is my string with &quot; inside &quot;" //This will produce : This is my string with "inside" 
相關問題