.NET Framework 类库
TemplateControl.XPath 方法 (String)

注意:此方法在 .NET Framework 2.0 版中是新增的。

计算 XPath 数据绑定表达式。

命名空间:System.Web.UI
程序集:System.Web(在 system.web.dll 中)

语法

Visual Basic(声明)
Protected Friend Function XPath ( _
    xPathExpression As String _
) As Object
Visual Basic(用法)
Dim xPathExpression As String
Dim returnValue As Object

returnValue = Me.XPath(xPathExpression)
C#
protected internal Object XPath (
    string xPathExpression
)
C++
protected public:
Object^ XPath (
    String^ xPathExpression
)
J#
protected Object XPath (
    String xPathExpression
)
JScript
protected internal function XPath (
    xPathExpression : String
) : Object

参数

xPathExpression

要计算的 XPath 表达式。有关更多信息,请参见 XPathBinder

返回值

计算数据绑定表达式所得出的对象。
异常

异常类型条件

InvalidOperationException

数据绑定方法只能用于 Page 上包含的控件。

备注

XPath 方法使用 GetDataItem 方法调用 XPathBinder.Eval 方法以解析表达式计算所依据的 IXPathNavigable 对象引用。

示例

下面的代码示例演示如何在 Repeater 控件中使用 XPath 方法。

Visual Basic
<%@ Page Language="VB" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html  >
<head id="Head1" runat="server">
    <title>TemplateControl XPath Example</title>
</head>
<body>
    <h3>TemplateControl XPath and XPathSelect Example</h3>
    <form id="form1" runat="server">
    <div>
      <asp:XmlDataSource
        id="XmlDataSource1" 
        runat="server"
        XPath="contacts" 
        DataFile="contacts.xml" />    
      <asp:FormView 
        id="FormView1" 
        runat="server" 
        DataSourceID="XmlDataSource1">
        <ItemTemplate>
          <hr />
          <asp:Repeater 
            id="Repeater1" 
            runat="server" 
            DataSource='<%# XPathSelect("contact") %>' >
            <ItemTemplate>
              Name: <%# XPath("name") %> <br />
              Note: <%# XPath("note") %> <br />
              <hr />
            </ItemTemplate>
          </asp:Repeater>
        </ItemTemplate>
      </asp:FormView>
    </div>
    </form>
</body>
</html>
C#
<%@ Page Language="C#"%>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html  >
<head runat="server">
    <title>TemplateControl XPath and XPathSelect Example</title>
</head>
<body>
    <h3>TemplateControl XPath Example</h3>
    <form id="form1" runat="server">
    <div>
      <asp:XmlDataSource
        id="XmlDataSource1" 
        runat="server"
        XPath="contacts" 
        DataFile="contacts.xml" />    
      <asp:FormView 
        id="FormView1" 
        runat="server" 
        DataSourceID="XmlDataSource1">
        <ItemTemplate>
          <hr />
          <asp:Repeater 
            id="Repeater1" 
            runat="server" 
            DataSource='<%# XPathSelect("contact") %>' >
            <ItemTemplate>
              Name: <%# XPath("name") %> <br />
              Note: <%# XPath("note") %> <br />
              <hr />
            </ItemTemplate>
          </asp:Repeater>
        </ItemTemplate>
      </asp:FormView>
    </div>
    </form>
</body>
</html>

下面的 XML 测试数据可以用在上面的代码示例中。

<contacts>
   <contact id="1">
     <name>contact name 1</name>
     <note>contact note 1</note>
   </contact>
   <contact id="2">
     <name>contact name 2</name>
     <note>contact note 2</note>
   </contact>
</contacts>
平台

Windows 98、Windows 2000 SP4、Windows Server 2003、Windows XP Media Center Edition、Windows XP Professional x64 Edition、Windows XP SP2、Windows XP Starter Edition

.NET Framework 并不是对每个平台的所有版本都提供支持。有关受支持版本的列表,请参见系统要求

版本信息

.NET Framework

受以下版本支持:2.0
请参见

标记 :


Page view tracker