首页 > 编程 > .NET > 正文

ASP.NET2.0 GridView绑定XmlDocument

2024-07-10 13:09:52
字体:
来源:转载
供稿:网友

  asp.net 2.0提供了多种数据源,一般情况下,xmldatasource控件使用xml文件,下面的例子就是使用xmldocument对象进行数据绑定的一种方法。

  vb.net代码

<%@ page language="vb" %>
<script runat="server">
  protected sub page_load(byval sender as object, byval e as system.eventargs)
    dim doc as new system.xml.xmldocument
    doc.load("http://dotnet.aspx.cc/rss.aspx")
    xmldatasource1.data = doc.innerxml
    xmldatasource1.xpath = "/rss/channel/item"
  end sub
</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<body>
  <form id="form1" runat="server">
    <asp:gridview id="gridview1" runat="server" datasourceid="xmldatasource1" autogeneratecolumns="false">
      <columns>
        <asp:templatefield headertext="文章标题">
          <itemtemplate>
            <asp:hyperlink runat="server" target="_blank" navigateurl='<%#xpath("link") %>'>
            <%#xpath("title")%></asp:hyperlink>
            [<%#ctype(xpath("pubdate"), datetime).tostring("yyyy年m月d日")%>]
          </itemtemplate>
        </asp:templatefield>
      </columns>
    </asp:gridview>
    <asp:xmldatasource id="xmldatasource1" runat="server"></asp:xmldatasource>
  </form>
</body>
</html>

  c#代码

<%@ page language="c#" autoeventwireup="true"%>
<script runat="server">
  protected void page_load( object sender, system.eventargs e )
  {
    system.xml.xmldocument doc = new system.xml.xmldocument();
    doc.load("http://dotnet.aspx.cc/rss.aspx");
    xmldatasource1.data = doc.innerxml;
    xmldatasource1.xpath = "/rss/channel/item";
  }
</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<body>
  <form id="form1" runat="server">
    <asp:gridview id="gridview1" runat="server" datasourceid="xmldatasource1" autogeneratecolumns="false">
      <columns>
        <asp:templatefield headertext="文章标题">
          <itemtemplate>
            <asp:hyperlink id="hyperlink1" runat="server" target="_blank" navigateurl='<%#xpath("link") %>'>
            <%#xpath("title")%></asp:hyperlink>
            [<%#(datetime.parse(xpath("pubdate").tostring().replace("gmt",""))).tostring("yyyy年m月d日")%>]
          </itemtemplate>
        </asp:templatefield>
      </columns>
    </asp:gridview>
    <asp:xmldatasource id="xmldatasource1" runat="server"></asp:xmldatasource>
  </form>
</body>
</html>

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表