views:

67

answers:

2

I would like to do something like:

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

but I don't think its possible. There is a way to create method in codebehind which returns appropriate string and call it, but thats not an option.

A: 

Its not possible to use # eval in if statement,

You have some options to solve that:

  • You can put the condition of the if in a previous line then check on this variable in the if

example: in code behind:

protected bool isDisabled;

in aspx:

<%# isDisabled=(bool)Eval("IsDisabled")  %>
<% if(isDisabled) %>
  • Other way is to call a code behind method which return bool and check on it in the if.
Amr ElGarhy
It appears that i cannot declare a variable in databing expression neither. "Invalid expression term 'bool'"
ni5ni6
i edited my answer to solve this problem.
Amr ElGarhy
good point! just one remark: I set isDisabled variable as public and it worked. Thats how MSDN says ;) Thanks a lot Amr! (if you modify your answer, I'll accept it)
ni5ni6
public will make the variable accessed from any where, but all what you need is to just from the aspx so protected is the right access, no need to make it public.
Amr ElGarhy
you are right, it works with protected as well.Thanks again!
ni5ni6
+1  A: 

You can use placeholders to hold the two versions of your markup and then use the Visible property to show the relevant one. Something like this... Note the use of ! before the call to IsDisabled in 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 code behind IsDisabled method looks like this...

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

Si Keep
nice solution as well
Amr ElGarhy