Export (0) Print
Expand All

XPathBinder.Eval Method (Object, String)

Evaluates XPath data-binding expressions at run time.

Namespace:  System.Web.UI
Assembly:  System.Web (in System.Web.dll)

public static Object Eval(
	Object container,
	string xPath
)

Parameters

container
Type: System.Object

The IXPathNavigable object reference that the expression is evaluated against. This must be a valid object identifier in the page's specified language.

xPath
Type: System.String

The XPath query from container to the property value that is placed in the bound control property.

Return Value

Type: System.Object
An Object that results from the evaluation of the data-binding expression.

ExceptionCondition
ArgumentNullException

The container or xpath parameter is null.

ArgumentException

The object specified by container is not an IXPathNavigable object.

You can use the Eval method declaratively if you want to simplify the casting of an XML node to a text string to be displayed on a browser. To do so, you must place the <%# and %> tags, which are also used in standard ASP.NET data binding, around the data-binding expression.

For any of the list ASP.NET server controls, such as DataList, DataGrid, or Repeater, the container parameter should be Container.DataItem. If you are binding against the page, the container parameter should be Page.

The following code example demonstrates how to use an XmlDataSource control with a templated Repeater control to display XML data. This example has two parts:

  • A Web Forms page that displays XML data.

  • An XML file that contains the data.

The first part of the example shows a Web Forms page that displays XML data accessed through an XmlDataSource control. A Repeater control uses the simplified Eval(Object, String) method syntax to bind to data items within the XML document that the XmlDataSource represents. It uses the Select(Object, String) method to retrieve an IEnumerable list and assign it as a late-bound DataSource property for the Repeater control.

<%@ Page Language="C#" %>
<!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>ASP.NET Example</title>
</head>
<body>
    <form id="form1" runat="server">
      <asp:XmlDataSource
        runat="server"
        id="XmlDataSource1"
        DataFile="bookstore2.xml"
        XPath="bookstore/genre[@name='fiction']" />

      <asp:Repeater
        runat="server"
        DataSourceID="XmlDataSource1">
        <ItemTemplate>
            <h1><%# XPath ("book/title") %></h1>
            <b>Author:</b>
            <%# XPath ("book/author/firstname") %>
            <%# XPath ("book/author/lastname") %>
            <asp:Repeater
                 DataSource='<%# XPathSelect ("book/chapters") %>'
                 runat="server">
                <ItemTemplate>
                     <%# XPath ("chapter/@name") %>
                </ItemTemplate>
            </asp:Repeater>
        </ItemTemplate>
    </asp:Repeater>
  </form>
  </body>
</html>

The second example provides the XML file, Bookstore2.xml, that is used as the source of the data displayed in the Web Forms page defined above.

<?xml version="1.0" encoding="iso-8859-1"?>
  <bookstore>
    <genre name="fiction">
      <book ISBN="10-861003-324">
        <author>
          <firstname>test</firstname>
          <lastname>test2</lastname>
        </author>
        <title>The Handmaid's Tale</title>
        <price>12.95</price>
        <chapters>
          <chapter num="1" name="Introduction" /> 
          <chapter num="2" name="Body" /> 
          <chapter num="3" name="Conclusion" />
        </chapters>
      </book>
    </genre>
    <genre name="nonfiction">
      <book genre="nonfiction" ISBN="1-861001-57-5">
        <author>
          <firstname>test3</firstname>
          <lastname>test4</lastname>
        </author>    
        <title>Pride And Prejudice</title>
        <price>24.95</price>
        <chapters>
          <chapter num="1" name="Introduction" /> 
          <chapter num="2" name="Body" />
          <chapter num="3" name="Conclusion" />
        </chapters>
      </book>
    </genre>
  </bookstore>

.NET Framework

Supported in: 4.6, 4.5, 4, 3.5, 3.0, 2.0

Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Show:
© 2014 Microsoft