之间的'(单引号)差值和“(双引号)在ASP.NET 4 [英] Difference between ' (single quote) and “ (double quote) in ASP.NET 4
问题描述
我想从服务器控制aspx页面调用的MyMethod在code,后面像下面。
MyPage.aspx
< ASP:标签ID =MyLabel=服务器文本='<%#的MyMethod(EVAL(MyColumn))%>'>
MyPage.aspx.cs
保护无效的MyMethod(obj对象){...}
如果我用而不是
在aspx页面,然后它会给我一个编译错误的的服务器标记的格式不正确。如下图所示。
< ASP:标签ID =MyLabel=服务器文本='<%#的MyMethod(EVAL(MyColumn))%>'> //这一行工作
< ASP:标签ID =MyLabel=服务器文本=<%#的MyMethod(EVAL(MyColumn))%GT;> //此行错误
我想知道为什么我需要使用单引号,是它的规则?如何使用双引号在我的处境?
我想知道为什么我需要使用单引号,是它的规则?我怎么能够
使用双引号在我的处境?
块引用>在双引号中使用单引号的就是要清楚其中字符串正在结束。不能使用文本=的MyMethod(123),因为文字开始与M和可与(或3或最后)结束。通过使用单引号和双引号,编译器知道该字符串结束的时候。
文本=的MyMethod('123')文字='的MyMethod(123)
您的例子是关于绑定,但让说你想有一个双引号,同时使用双报价不具约束力的局面。你可以有使用HTML实体"
文本=这是我的字符串和放大器; QUOT;内部和放大器; QUOT; //这将产生:这是我的字符串内
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?
解决方案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"
这篇关于之间的'(单引号)差值和“(双引号)在ASP.NET 4的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!