How to assign non string values in markup? RRS feed

  • Question

  • User1445529928 posted


    I have a server control DbSourceControl, that is a datasource control for EF CodeFirst, and has a property Where of type Func<IQueryable, IQueryable>. How do I set Where from markup?

    I've tried like so:

    <ss:DbSourceControl runat='server' Where='<%# (Func<IQueryable, IQueryable>)(set => set.OfType<MyEntityType>().Where(ad => MyWhereExpression))) %>' />

    But the Where property doesn't get assigned. Also Eval does not work, cause Eval expects a string.

    Neither does <%=, it says it can't convert Func to string.

    How can I solve this?

    BTW: If I get this to work, I could post the DbDataSource on Codeplex, as it is very nice to work with it & EF CodeFirst.

    Tuesday, August 27, 2013 7:00 AM


  • 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

    #Using Expression Builders in ASP.NET

    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)
            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">
        <form id="form1" runat="server">
            <uc1:WebUserControl1 runat="server" id="WebUserControl1" 
                Text1="<%# this.Data %>"  TextFunc="<%# this.Func1  %>"

    ## 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;




    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, August 28, 2013 2:37 AM