I want to call MyMethod in code-behind from server control in aspx page like 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 '
in aspx page then it will give me a compilation error The server tag is not well formed. as below.
<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 single-quote, is it a rule? How can I use double-quote in my situation?
General Usage Rules In America, Canada, Australia and New Zealand, the general rule is that double quotes are used to denote direct speech. Single quotes are used to enclose a quote within a quote, a quote within a headline, or a title within a quote.
As far as language syntax is concerned, there is no difference in single or double quoted string. Both representations can be used interchangeably. However, if either single or double quote is a part of the string itself, then the string must be placed in double or single quotes respectively.
The single quote is used to define a character literal. The double quote is used to define a string literal. Neither of them have any other purpose in the C# language.
Generally, there is no difference between using double or single quotes, as both of them represent a string in the end. There is only one difference in the usage of single and double quotes, and it comes down to what quote character you need to escape using the backslash character (\): \' or \”.
I want to know why I need to use single-quote, is it a rule? How can I use double-quote in my situation?
The use of Single quote over Double quote is just to make it clear where the string is ending. You cannot use Text="MyMethod("123")" because the Text start with the M and may end with the ( or the 3 or the last ). By using single and double quote the compiler know when the string end.
Text="MyMethod('123')"
Text='MyMethod("123")'
Your example is about binding but let say that you would like to have a double quote while using a double quote for a non-binding situation. You could have use the HTML entity "
Text="This is my string with " inside "" //This will produce : This is my string with "inside"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With