gpt4 book ai didi

asp.net - ASP.NET 4 中 '(单引号)和“(双引号)之间的区别

转载 作者:行者123 更新时间:2023-12-04 12:12:34 25 4
gpt4 key购买 nike

我想在 aspx 页面中的服务器控件的代码隐藏中调用 MyMethod,如下所示。

我的页面.aspx

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

我的页面.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

我想知道为什么我需要使用单引号,这是一个规则吗?在我的情况下如何使用双引号?

最佳答案

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



使用单引号而不是双引号只是为了清楚字符串的结束位置。您不能使用 Text="MyMethod("123")"因为 Text 以 M 开头并且可能以 ( 或 3 或最后一个 ) 结尾。通过使用单引号和双引号,编译器知道字符串何时结束。
Text="MyMethod('123')"

Text='MyMethod("123")'

您的示例是关于绑定(bind)的,但是假设您希望使用双引号,而在非绑定(bind)情况下使用双引号。您可以使用 HTML 实体“
Text="This is my string with  &quot; inside &quot;" //This will produce : This is my string with "inside"

关于asp.net - ASP.NET 4 中 '(单引号)和“(双引号)之间的区别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7433729/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com