locked
Optional Eval Binding RRS feed

  • Question

  • User-373518479 posted

    Dear All,

    I have a listview and I want to databind one label "Optionally" using the eval command. Is that possible? In other words; the datasource might contain that databound column and it might not. Can we say <asp:label id="lbl" runat="server" text=OPTIONAL '<% #Eval("ID") %>' ></label>

    Wednesday, November 7, 2018 10:02 AM

Answers

  • User-158764254 posted

    Add a new method to your page so you can control the Eval and prevent it for a missing property

    This method checks for the existence of the property before it runs DataBinder.Eval

    If the property name passed as the expression does not exist, it returns whatever value you pass for defaultWhenMissing 

        public object EvalOptional(object container, string expression, string defaultWhenMissing)
        {
            if (container.GetType().GetProperty(expression) != null)
                return DataBinder.Eval(container, expression);
            else
                return defaultWhenMissing;
        }

    Then change your markup to call this custom version of eval 

    <asp:label id="lbl" runat="server" text='<% #EvalOptional(Container.DataItem,"ID","yourdefault") %>' ></label>

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, November 8, 2018 1:18 PM

All replies

  • User475983607 posted

    Dear All,

    I have a listview and I want to databind one label "Optionally" using the eval command. Is that possible? In other words; the datasource might contain that databound column and it might not. Can we say <asp:label id="lbl" runat="server" text=OPTIONAL '<% #Eval("ID") %>' ></label>

    A bound column is a one-to-one relationship. It is not possible have a bound column and a "might not"  bound column.  You can have a default message is the bound column is NULL or you can have two bound columns where only one is shown.

    Wednesday, November 7, 2018 2:46 PM
  • User839733648 posted

    Hi ahmadbanat,

    Usually, when you want to bind the datas from database to a control like listview, you will have to bind columns to columns.

    The type of each column is the same, either a fixed data column or a bound data column.

    You could refer to the official document to know more abou listview: ListView Web Server Control Overview

    You could refer to the link to try the sample: Display (Show) data in ListView from database in ASP.Net using C# and VB.Net

    Best Regards,

    Jenifer

    Thursday, November 8, 2018 10:02 AM
  • User-158764254 posted

    Add a new method to your page so you can control the Eval and prevent it for a missing property

    This method checks for the existence of the property before it runs DataBinder.Eval

    If the property name passed as the expression does not exist, it returns whatever value you pass for defaultWhenMissing 

        public object EvalOptional(object container, string expression, string defaultWhenMissing)
        {
            if (container.GetType().GetProperty(expression) != null)
                return DataBinder.Eval(container, expression);
            else
                return defaultWhenMissing;
        }

    Then change your markup to call this custom version of eval 

    <asp:label id="lbl" runat="server" text='<% #EvalOptional(Container.DataItem,"ID","yourdefault") %>' ></label>

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, November 8, 2018 1:18 PM