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:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<%@ Register Src="~/MyUserControl.ascx" TagName="MyUserControl" TagPrefix="uc1" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<uc1:MyUserControl ID="MyUserControl1" runat="server"
UserControlProperty = '<%# Bind("PageProperty") %>' />
</div>
</form>
</body>
</html>
Here is the code behind (aspx.cs) file from the aspx page:
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;
}
}
Here is the markup from the usercontrol (ascx file):
<%@ Control Language="C#" AutoEventWireup="true"
CodeFile="MyUserControl.ascx.cs" Inherits="MyUserControl" %>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
And here is the code behind file (ascx.cs) from the user control:
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();
}
}
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?