User-742633084 posted
Hi simonegli,
So your custom web server control has a function expression property which need to be assigned (via markup or codebehind) at runtime and you encounter some problem assigning it from aspx markup, correct?
According to the code snippet you mentioned, you've tried <%# %> and some other inline expressions. Personally, I think the following two inline expressions should work for your case:
1) the data binding expression like <%# %> which you've tried. However, to make it work, you need to call "DataBind" method on the custom control to as to get the expression executed.
2) the custom expression builder like <%$ %> should also works. You can create custom expression builder so as to invoke customer helper functions in aspx markup inline (do not need to invoke .DataBind method or other method additionally).
Here are some reference for your information.
#Introduction to ASP.NET inline expressions in the .NET Framework
http://support.microsoft.com/kb/976112/en-us
#Using Expression Builders in ASP.NET
http://www.4guysfromrolla.com/articles/022509-1.aspx
I've also created a simple ASCX usercontrol for test which uses data binding expression to assign a custom property of function expression type. here is the test code:
##code behind of ascx control:
public partial class WebUserControl1 : System.Web.UI.UserControl
{
public string Text1
{ get;set;}
public Func<string, string> TextFunc { get; set; }
protected void Page_Load(object sender, EventArgs e)
{
}
protected override void OnDataBinding(EventArgs e)
{
base.OnDataBinding(e);
if (string.IsNullOrEmpty(Text1))
{
Response.Write("<br/>WebUserControl1.Text1 is empty.");
}
if (TextFunc == null)
{
Response.Write("<br/>WebUserControl1.TextFunc is empty.");
}
Label1.Text = Text1;
}
}
## aspx markup of test page (which host the usercontrol)
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<uc1:WebUserControl1 runat="server" id="WebUserControl1"
Text1="<%# this.Data %>" TextFunc="<%# this.Func1 %>"
/>
</div>
</form>
</body>
</html>
## codebehind of the test aspx page
public partial class WebForm1 : System.Web.UI.Page
{
public string Data { get; set; }
public Func<string, string> Func1{get;set;}
protected void Page_Load(object sender, EventArgs e)
{
Data = DateTime.Now.ToLongTimeString();
Func1 = (s) => s + "+" + s;
WebUserControl1.DataBind();
}
}