5

Attention: Another post(Set Visible property with server tag <%= %> in Framework 3.5) provides a more verbose answer to this question too.

I'm curious why the inline code does not behave as the code-behind in this case.

I have a class that contains settings as follows:

// Collection of Settings
public static class FeatureControl
{
     public static bool SettingName = true;
}

Code Behind executes as expected.

Label1.Visible = FeatureControl.SettingName; //true
Label2.Visible = !FeatureControl.SettingName; //false

Inline Code always shows both labels, regardless of the SettingName's value:

<asp:Label ID="Label1" Visible="<%#FeatureControl.SettingName%>"  runat="server" >  </asp:Label>
<asp:Label ID="Label2" Visible="<%#FeatureControl.SettingName != true %>"  runat="server" ></asp:Label>
Community
  • 1
  • 1
Roman Mik
  • 3,179
  • 3
  • 27
  • 49
  • @MatthewHaugen The change causes {"Cannot create an object of type 'System.Boolean' from its string representation '<%:FeatureControl.SettingName%>' for the 'Visible' property."} – Roman Mik Jul 31 '14 at 19:16
  • Alright, never mind then. Hmm. – Matthew Haugen Jul 31 '14 at 19:18
  • 1
    See this question: [set-visible-property-with-server-tag-in-framework-3-5](http://stackoverflow.com/questions/9595851/set-visible-property-with-server-tag-in-framework-3-5) – j.f. Jul 31 '14 at 19:20
  • Thank you for the link @j.f. however, the answer points to what I'm already doing...as option 1. What is strange, the Visibility value seem to be always true for me. – Roman Mik Jul 31 '14 at 19:25

2 Answers2

1

Since <%# %> is a databinding expression, I'm pretty sure you have to call Page.DataBind(). Give it a try:

protected void Page_Load(object sender, EventArgs e) 
{
     DataBind();
}
mxmissile
  • 11,464
  • 3
  • 53
  • 79
-3

The reason your inline code is not working is because != is a comparison operator not an assignment operator. You are not setting it to false.

arserbin3
  • 6,010
  • 8
  • 36
  • 52
MikeV
  • 585
  • 3
  • 11
  • Mike, != was for demonstration only. If I set my SettingName to false in the static class, the Label1 is still visible. – Roman Mik Jul 31 '14 at 19:17
  • How is this relevant? The OP clearly isn't trying to set it to false. – Matthew Haugen Jul 31 '14 at 19:19
  • @Mathew Either my English is terrible or the OP is actually asking why when the value of the Visible property is false the control is still visible. In the code behind he is using a NOT operator to set the default true value to false. Using =! (does not equal) is not the same as using ! (not). – MikeV Jul 31 '14 at 19:28
  • @MikeV you're correct, but even if I use !FeatureControl.SettingName I still get the same behavior. My Label is visible when it should not be – Roman Mik Jul 31 '14 at 19:31
  • @MikeV Given a `bool value`, `value != true` is equivalent to `!value`. The latter is nothing more than syntactic sugar for the first, really. The first is just like saying `int x = 10; return x != 10;`, except we're dealing with `bool` and not `int`. `!=` evaluates the `bool` value compared to the constant `true` and returns whether they are equal, thus whether the `bool` is *not* set to `true`. – Matthew Haugen Jul 31 '14 at 20:24
  • @Mathew Haugen thanks for the clarification. I thought that a comparison operator would require an actual comparison (like an if statement) in order to compare two values. And since the OP was using the comparison operator in the value of a property on a lable, I thought he was trying to set it to false so the lable would not be visible. – MikeV Jul 31 '14 at 20:35
  • @MikeV I see, yeah, you can set a variable or property equal to an expression, and both unary and binary operators qualify as expressions. – Matthew Haugen Jul 31 '14 at 20:38