How to mix embedded code blocks with data-binding expression

.net, asp.net, c#

Solution

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:

`<%# 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")) %>`

Problem

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. I want to use base <% ... %> embedded code blocks with <%# ... %> data-binding expression Example I want to add an "If" condition to this code: `<asp:Label ID="lblHello" runat="server" Text="<%# DataBinder.Eval(Container.DataItem, "[\"Txt\"]")%>"></asp:Label>` Regards.

Original source