Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Inline code is not executing as the code behind

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>
like image 728
Roman Mik Avatar asked Nov 26 '22 15:11

Roman Mik


1 Answers

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();
}
like image 90
mxmissile Avatar answered Dec 04 '22 11:12

mxmissile