title: Why will <%= %> expressions as property values on a server-controls lead to a compile errors?
body: <p>This question is a result of what i noticed while trying to answer <a href="https://stackoverflow.com/questions/369736/assigning-visible-property-of-the-button-to-a-static-method-result">another question</a>. And now im curious to know why <code>&lt;asp:TextBox runat="server" Visible="&lt;%= true %&gt;" /&gt;</code> leads to a compile error, and not to a visible TextBox as i would have expected.</p>
  
  <p>From what i have discovered so far, the <code>&lt;%= %&gt;</code> expressions are not translated to literal controls as i have always thought. But instead it is evaluated and written directly to the HtmlTextWriter when the page is rendered. But apparently the parser (I'm unsure that is the correct term for the part that is translating ASP.NET markup to .NET code) doesn't even attempt to evaluate <code>&lt;%= %&gt;</code> expressions, when they are used as property values for server controls. It just uses it as a string. Which i guess is why I get the error message: <em>Cannot create an object of type 'System.Boolean' from its string representation '&lt;%= true %>' for the 'Visible' property</em>. </p>
  
  <p>If i instead ditch the runat="server" and combines the <code>&lt;%= %&gt;</code> with regular html-markup, like this:</p>
  
  <pre><code>&lt;input type="button" id="Button1" visible='&lt;%= true %&gt;' /&gt;
  </code></pre>
  
  <p>Then the parser just splits the chunk in parts before and after the expression and then writes it to the HtmlTextWriter in the render method. Something like this:</p>
  
  <pre><code>    __w.Write("&lt;input type=\"button\" id=\"Button1\" visible='");
      __w.Write(true);
      __w.Write("' /&gt;");
  </code></pre>
  
  <p>As the last thing i noticed... When i try with <code>&lt;%# %&gt;</code> + Control.DataBind(), then i get what i would expect. It hooks up the expression to be used when the control is databound, but unlike the &lt;%= %> expression, the generated code actually evaluates the content of the <code>&lt;%# %&gt;</code> expression. The parser ends up generating the following:</p>
  
  <pre><code>[DebuggerNonUserCode]
  private Button __BuildControldataboundButton()
  {
      Button button = new Button();
      base.databoundButton = button;
      button.ApplyStyleSheetSkin(this);
      button.ID = "databoundButton";
      button.DataBinding += new EventHandler(this.__DataBindingdataboundButton);
      return button;
  }
  
  public void __DataBindingdataboundButton(object sender, EventArgs e)
  {
      Button button = (Button) sender;
      Page bindingContainer = (Page) button.BindingContainer;
      button.Visible = true;
  }
  </code></pre>
  
  <p>From:</p>
  
  <pre><code>&lt;asp:Button ID="databoundButton" Visible='&lt;%# true %&gt;' runat="server" /&gt;
  </code></pre>
  
  <p>Notice the <code>button.Visible = true;</code> that is the result of the <code>&lt;%# %&gt;</code> expression.</p>
  
  <p>So my question is.. Why is the expression in the first example just treated as a string, instead of being evaluated to "true". The expressions is somewhat similar for the two other examples, and they yield the code i would have expected.</p>
  
  <p>Is it just a mistake (which i doubt since it isn't a new issue with the current version of ASP.NET), or is there a good reason why we are not allowed to use <code>&lt;%= %&gt;</code> like that?</p>