title: ASP.Net Declaratively Passing Value from Page Property to User Control
body: <p>I need to find a way to declaratively (not in the code behind file) pass the value of a property in an ASP.Net web page to a user control.  Following is a simple example of what I'm trying to do, but I can't get it to work.
  Here is the markup for the aspx page where I'm creating an object of the user control:</p>
  
  <pre><code>&lt;%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs"  Inherits="_Default" %&gt;
  &lt;%@ Register Src="~/MyUserControl.ascx" TagName="MyUserControl" TagPrefix="uc1" %&gt;
  
  &lt;!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt;
  
  &lt;html xmlns="http://www.w3.org/1999/xhtml"&gt;
  &lt;head runat="server"&gt;
      &lt;title&gt;&lt;/title&gt;
  &lt;/head&gt;
  &lt;body&gt;
      &lt;form id="form1" runat="server"&gt;
      &lt;div&gt;
      &lt;uc1:MyUserControl ID="MyUserControl1" runat="server"
        UserControlProperty = '&lt;%# Bind("PageProperty") %&gt;' /&gt;
      &lt;/div&gt;
      &lt;/form&gt;
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>Here is the code behind (aspx.cs) file from the aspx page:</p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Web;
  using System.Web.UI;
  using System.Web.UI.WebControls;
  
  public partial class _Default : System.Web.UI.Page 
  {
      public int PageProperty { get; set; }
  
      protected void Page_Load(object sender, EventArgs e)
      {
          PageProperty = 42;
  
      }
  }
  </code></pre>
  
  <p>Here is the markup from the usercontrol (ascx file):</p>
  
  <pre><code>&lt;%@ Control Language="C#" AutoEventWireup="true" 
  CodeFile="MyUserControl.ascx.cs" Inherits="MyUserControl" %&gt;
  
  &lt;asp:TextBox ID="TextBox1" runat="server"&gt;&lt;/asp:TextBox&gt;
  </code></pre>
  
  <p>And here is the code behind file (ascx.cs) from the user control:</p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Web;
  using System.Web.UI;
  using System.Web.UI.WebControls;
  
  public partial class MyUserControl : System.Web.UI.UserControl
  {
      public int UserControlProperty { get; set; }
  
  
      protected void Page_Load(object sender, EventArgs e)
      {
          TextBox1.Text = UserControlProperty.ToString();
      }
  }
  </code></pre>
  
  <p>So, all I'm trying to do is pass the value of the PageProperty property defined in the aspx page into the user control to set it's UserControlProperty, and then display that value in a textbox in the usercontrol.  Can anyone tell me what I've done wrong here?</p>