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?
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.
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 "