职业IT人-IT人生活圈

 找回密码
 成为会员
搜索
查看: 1219|回复: 0

ASP.NET2.0 GridView绑定XmlDocument

[复制链接]
joe 发表于 2007-1-25 16:07 | 显示全部楼层 |阅读模式
  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>
您需要登录后才可以回帖 登录 | 成为会员

本版积分规则

QQ|手机版|小黑屋|网站帮助|职业IT人-IT人生活圈 ( 粤ICP备12053935号-1 )|网站地图
本站文章版权归原发布者及原出处所有。内容为作者个人观点,并不代表本站赞同其观点和对其真实性负责,本站只提供参考并不构成任何投资及应用建议。本站是信息平台,网站上部分文章为转载,并不用于任何商业目的,我们已经尽可能的对作者和来源进行了通告,但是能力有限或疏忽造成漏登,请及时联系我们,我们将根据著作权人的要求立即更正或者删除有关内容。

GMT+8, 2024-5-12 04:08 , Processed in 0.145979 second(s), 20 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表