Is it possible to use data binding expressions directly in markup to show / hide content?

I would like to do something like:

<%# if((bool)Eval("IsDisabled")){ %><span>Disabled</span><% } 
else { %><span>Active</span><% } %>

      

but I don't think it is possible. There is a way to create a method in codebehind that returns the appropriate string and calls it, but that is not an option.

+2


source to share


2 answers


Can't use # eval in if statement,

You have several options for solving this problem:

  • You can put an if condition on the previous line and then check that variable in the if

Example: in the code behind:



protected bool isDisabled;

      

in aspx:

<%# isDisabled=(bool)Eval("IsDisabled")  %>
<% if(isDisabled) %>

      

  • Another way is to call the code behind the method that returns a bool and tests it in an if.
0


source


You can use placeholders to hold two versions of your markup, and then use the Visible property to display the appropriate one. Something like this ... Note the usage! before calling IsDisabled on the second Visible property.

<asp:PlaceHolder ID="PlaceHolder1" runat="server" Visible='<%# IsDisabled((bool) Eval("IsDisabled")) %>'>
            <span>Disabled</span>
</asp:PlaceHolder>
<asp:PlaceHolder ID="PlaceHolder2" runat="server" Visible='<%#  !IsDisabled((bool) Eval("IsDisabled")) %>'>
            <span>Active</span>
</asp:PlaceHolder>

      



The IsDisabled method code looks like this:

public bool IsDisabled (bool isDisabled)
{
   return isDisabled;
}

      

+2


source







All Articles