I'm trying to use data binding to disable a button in ASP.NET, but the button remains enabled. What am I doing wrong? Here is my example program which demonstrates the problem.
Default.aspx:
<%@ Page Title="Home Page" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="WebApplication1._Default" %>
<asp:Content ID="BodyContent" ContentPlaceHolderID="MainContent" runat="server">
<div>
<asp:Button runat="server" Text="Button 1" Enabled="<%# false %>" /><br />
<asp:Button runat="server" Text="Button 2" Enabled="<%# PropertyReturnsFalse %>" /><br />
<asp:Button runat="server" Text="Button 3" Enabled="<%# StaticPropertyReturnsFalse %>" /><br />
<asp:Button runat="server" Text="Button 4" Enabled="true" /><br />
<asp:Button runat="server" Text="Button 5" Enabled="false" /><br />
</div>
</asp:Content>
Default.aspx.cs:
using System;
using System.Web.UI;
namespace WebApplication1
{
public partial class _Default : Page
{
public bool PropertyReturnsFalse
{
get { return false; }
}
public static bool StaticPropertyReturnsFalse
{
get { return false; }
}
protected void Page_Load(object sender, EventArgs e)
{
}
}
}
I expect button 4 to be enabled and buttons 1, 2, 3, and 5 to be disabled. The actual result at run time is that buttons 1, 2, 3, and 4 are enabled, and only button 5 is disabled.
I know ASP.NET is matching the property names in the ASPX file to the code-behind, because if I change the ASPX file to a mismatch, I get an error. I get no error when the names match.
I set breakpoints on the property implementations. Neither breakpoint gets hit at run time.