Difference between '(single quote) and "(double quote) in ASP.NET 4

I want to call MyMethod in the server control code on an aspx page, as shown below.

Mypage.aspx

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

MyPage.aspx.cs

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

If I use " instead of ' in an aspx page, this will give me a compilation error. The server tag is not formed .

 <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 

I want to know why I need to use a single quote, is this a rule? How to use double quote in my situation?

+6
source share
1 answer

I want to know why I need to use a single quote, is this a rule? How can I use double quote in my situation?

Using a single quote over a double quote is simply to indicate where the line ends. You cannot use Text = "MyMethod (" 123 ")" because the text starts with M and can end with (or 3 or the last). Using single and double quotes, the compiler knows when the line ends.

 Text="MyMethod('123')" Text='MyMethod("123")' 

Your example is about binding, but let's say that you would like to have a double quote, using a double quote for an optional situation. You could use an HTML object "

 Text="This is my string with &quot; inside &quot;" //This will produce : This is my string with "inside" 
+6
source

Source: https://habr.com/ru/post/897401/


All Articles