如何用XML数据绑定前台Repeater

  最近有朋友问到我如何用XML数据绑定前台Repeater文件,我查了一下以前做的项目,这里给大家介绍一下,方便大家也方便我自己日后查询。

  废话少说,上代码:

  前台文件(.aspx):

  在aspx前台页面引用XML命名空间:

 <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="exam_Review.aspx.cs" Inherits="HaiTian.Exam.exam_Review" %>
<%@ Import Namespace="System.Xml" %>

在Repeater中绑定数据如下:

 <asp:Repeater runat="server" ID="rptList">
                <ItemTemplate>
                    <tr>
                        <td width="5%" colspan="1">
                        </td>
                        <td width="5%" colspan="1" align="center">
                           <span class="spanSign">
                                <%#((XmlNode)Container.DataItem).ChildNodes[7].InnerText%>1</span>
                        </td>
                        <td width="10%" colspan="1" align="center">
                            <%#(int.Parse(((XmlNode)Container.DataItem).ChildNodes[8].InnerText)+1)%>
                        </td>
                        <td width="80%" colspan="1">
                            <a href="#" onclick="reviewTitle(<%#(int.Parse(((XmlNode)Container.DataItem).ChildNodes[8].InnerText)+1)%>)"; id="review">返回问题</a>
                        </td>
                    </tr>
                </ItemTemplate>
            </asp:Repeater>

后台文件(.cs):

  protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (Fetch.Get("action") == "Run")//正在运行
                {
                       //为Repeater控件绑定XML数据源

                      //这里this.hdtitCollect.Value为XML文件的路径
                    BindRepeateByXML(this.hdtitCollect.Value);
                }
            }

 

     /// <summary>
        /// 用XML数据源来绑定Repeater
        /// </summary>
        /// 2011-08-15
        /// <param name="path">XML文件的路径</param>
        public void BindRepeateByXML(string path)
        {
            XmlDocument xmldoc = new XmlDocument();
            xmldoc.Load(Server.MapPath(path));

           //这里rptList为前台(aspx)页面Repeater的id
            this.rptList.DataSource = xmldoc.DocumentElement.ChildNodes;
            this.rptList.DataBind();
        }

 

posted @ 2012-07-28 16:16  TealerProg  Views(245)  Comments(0Edit  收藏  举报