views:

1466

answers:

2

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"&gt;

<html xmlns="http://www.w3.org/1999/xhtml"&gt;
<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?

+2  A: 

try

UserControlProperty = '<%= this.PageProperty %>'

or consider reading: http://support.microsoft.com/kb/307860#1b there's such a thing as Page.DataBind() and Control.DataBind(). I'm not quite sure if you should call them explicitly, but it might be the case...

if you still want your case, you can try to do it through string:

public string UserControlProperty { get; set; }

it perfectly works.

ifesdjeen
Good suggestion, but it gives a compile error.
Russ Clark
just a minute. checking this case...
ifesdjeen
just updated my comment :)=
ifesdjeen
also, since you have a back-end in any case, you can just declare similar definition into the code-behind... MyUserControl1.UserControlProperty = 124
ifesdjeen
A: 

I would recommend taking a look at this article.

It shows you how to create your own ExpressionBuilder to parse C# anywhere in your markup without having to DataBind or jump through any other hoops.

Chris Shouts