在内联代码 ASP.NET 中连接两个或多个字符串
我试图根据条件在名称旁边放置一个 * 。
我的代码:
<asp:Label ID="lblOne" runat="server" Text= '<%# Eval("name") + ((Eval("StatusId").Equals(0) && Eval("assignfilename") == null) ? " *" : "") %>' > </asp:Label>
谢谢
BB
I am trying to place a * next to the name based on a condition.
My code :
<asp:Label ID="lblOne" runat="server" Text= '<%# Eval("name") + ((Eval("StatusId").Equals(0) && Eval("assignfilename") == null) ? " *" : "") %>' > </asp:Label>
Thanks
BB
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
我对内联代码不太熟悉,并且您的代码似乎有点复杂。
但我还需要连接 Eval("record") 和文本。因此,要回答如何连接的问题,&符号对我有用。
希望这对任何人都有帮助。
I'm not really familiar with in-line codes and your code seems to be a bit complicated.
But I also need to concatenate an Eval("record") and a text. So to answer the question on how to concatenate, ampersand worked for me.
hope this helps anyone.
如果您正在突破使用内联代码可以轻松处理的限制,那么您始终可以编写一个函数。然后你可以做类似的事情:
这可以让你将一个复杂的表达式分解成它需要的任意多行,这可以不那么尴尬。您可以在 CodeBehind 或任何其他类中使用函数。
如果您要绑定到您有权访问的类,则可以添加只读属性。然后你可以做类似 Eval("MyNewProperty") 的事情。
我用它来公开我需要重复使用的格式。例如,Customer.CustomerFullName 可能会返回姓氏,首先用逗号分隔(智能处理其中一个或另一个或两者都缺失的情况)再加上一个可选的头衔,因为也许我的客户是医务人员,其中一些拥有博士学位和医学博士学位。
If you're pushing the limits of what you can easily handle with inline code, you could always write a function instead. Then you can do something like:
This lets you break a complex expression up into however many lines it needs to be, which can be a little less awkward. You can use a function in your CodeBehind or any other class.
If you're binding to a class that you have access to, you could add a readonly property. Then you can do something like Eval("MyNewProperty").
I use that for exposing formatting that I need to re-use. For instance, Customer.CustomerFullName might return last name first seperated be a comma (intelligently handling situations where one or the other or both are missing) plus an optional title, since maybe my customers are medical folks and some of them have PhDs and MDs.
对于简单的一次性场景,代码隐藏函数可以正常工作。
您可能还需要考虑将它们编码为基础对象中的属性。
例如,如果生成的文本将在多个实例中使用,则您需要在不同的表单或控件中多次使用 Evals 对该函数进行编码。
我会在数据对象上创建一个属性,例如 NameWithStatusStar,然后您的标签可以使用 Eval("NameWithStatusStar") 内部的代码直接绑定到该属性,
这比一系列表达式更具描述性和可重用性,而且更容易更改(例如添加另一个字段、更改公式等)
For simple one-off scenarios the code-behind function works okay.
You may also want to consider coding them as a property in the underlying object.
For example, if the text generated is going to be used in more than one instance, you'd need to code the function with Evals several times in different forms or controls.
I would create a property on the data object, e.g. NameWithStatusStar, then your label can be bound directly to the property with the code inside Eval("NameWithStatusStar")
This is more descriptive and reusable than a series of expressions, plus it's easier to change (e.g. add another field, change the formula etc.)
你可以这样做:
You can do it like this:
这在我的项目中对我有用。在这里找到它:
用 Eval 连接文本
This worked for me in my project. Found it here:
Concatenate text with Eval
这在我的项目中对我有用。在这里找到它:
用 Eval 连接文本
This worked for me in my project. Found it here:
Concatenate text with Eval