如何将嵌入式代码块与数据绑定表达式混合

时间:2021-02-11 14:01:52

I read this document http://support.microsoft.com/kb/976112 where explains all the embedded code blocks available but I want to combine two of them.

我阅读了这个文档http://support.microsoft.com/kb/976112,其中解释了所有可用的嵌入式代码块,但我想合并其中两个。

I want to use base <% ... %> embedded code blocks with <%# ... %> data-binding expression

我想用base <%…%>嵌套代码块<%#…% >数据绑定表达式

Example I want to add an "If" condition to this code:

例如,我想在此代码中添加一个“If”条件:

<asp:Label ID="lblHello" runat="server" Text="<%# DataBinder.Eval(Container.DataItem, "[\"Txt\"]")%>"></asp:Label>

< span lang = en - us style = ' font - size: 9.0 pt; font - family:宋体;mso - ascii - font - family: tahoma;DataItem,"[\“Txt \]")% > " > < / asp:Label >

Regards.

的问候。

1 个解决方案

#1


3  

The difference in the <% and <%# is mainly in when they're run (the former at render time, the latter at data binding). As such, it makes no sense to "combine" them.

<%和<%#的区别主要在于它们在运行时(在呈现时,后者在数据绑定上)。因此,“合并”它们是没有意义的。

What you likely want to do, is to run some additional code when data binding to do your if statement. If it's a simple expression, you can just inline it:

您可能希望在数据绑定时运行一些附加代码来执行if语句。如果它是一个简单的表达式,你可以将它内联:

<%# MyProperty ? Eval("Txt") : Eval("OtherTxt") %>

< % # MyProperty吗?Eval(Txt):Eval(OtherTxt)% >

If it's more complicated, then it's usually best to just call a code-behind method to do it for you:

如果它更复杂,那么最好直接调用代码隐藏方法来完成:

<%# MyMethod(Eval("Txt")) %>

< % # MyMethod(Eval(Txt))% >

#1


3  

The difference in the <% and <%# is mainly in when they're run (the former at render time, the latter at data binding). As such, it makes no sense to "combine" them.

<%和<%#的区别主要在于它们在运行时(在呈现时,后者在数据绑定上)。因此,“合并”它们是没有意义的。

What you likely want to do, is to run some additional code when data binding to do your if statement. If it's a simple expression, you can just inline it:

您可能希望在数据绑定时运行一些附加代码来执行if语句。如果它是一个简单的表达式,你可以将它内联:

<%# MyProperty ? Eval("Txt") : Eval("OtherTxt") %>

< % # MyProperty吗?Eval(Txt):Eval(OtherTxt)% >

If it's more complicated, then it's usually best to just call a code-behind method to do it for you:

如果它更复杂,那么最好直接调用代码隐藏方法来完成:

<%# MyMethod(Eval("Txt")) %>

< % # MyMethod(Eval(Txt))% >