zoukankan      html  css  js  c++  java
  • Repeater 的嵌套使用与表克隆

    这两天因为网站上有一个子父类别的显示。在编写的时候遇到一些问题,把它记录下来,以供参考:

    前端:

    <asp:Repeater ID="RepProTypeParent" runat="server"   OnItemDataBound="ProType_itemDataBound">
          <ItemTemplate>
               <div class="h_div">
                 <h3 class="font14"><a href="/Supply/Trade-<%#Eval("id")%>.aspx"><%#Eval("type")%></a></h3>
                 <asp:Repeater ID="RepProTypeChild" runat="server"><HeaderTemplate><div class="h_div1"></HeaderTemplate><ItemTemplate>
                 <a href="/Supply/Trade-<%#Eval("id")%>.aspx"><%#Eval("type")%></a></ItemTemplate><FooterTemplate></div></FooterTemplate></asp:Repeater>
               </div></ItemTemplate></asp:Repeater>

    后台主要代码:

    protected DataTable newTable=new DataTable();

    protected void Page_Load(object sender, EventArgs e)
      {
          if (!IsPostBack)
          {

             // proTypeTable 放在此读取数据为了减少从数据库读写数据的次数,达到优化的目的
              proTypeTable = newsInfo.GetDataTable("select id,type,SuperiorsID from w_industryType  where SuperiorsID<>0");
             newTable = proTypeTable.Clone();
            BindParent();

    }

    }

    //绑定父类

    protected void BindParent(){

        proTypeTables = newsInfo.GetDataTable("select id,type from industryType  where SuperiorsID=0");
        RepProTypeParent.DataSource = proTypeTables;
        RepProTypeParent.DataBind();
      }
      /// <summary>
      /// 绑定子类
      /// </summary>
      /// <param name="sender"></param>
      /// <param name="e"></param>
       protected void ProType_itemDataBound(object sender, RepeaterItemEventArgs e)
       {

           newTable=proTypeTable.clone();
           if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType==ListItemType.AlternatingItem)
           {
               Repeater RepChild = e.Item.FindControl("RepProTypeChild") as Repeater;
               DataRowView getRow = (DataRowView)e.Item.DataItem;
               DataRow[] drs=proTypeTable.Select("SuperiorsID="+getRow["id"]);
               Response.Write(getRow["id"].ToString()+"-");
               foreach(DataRow dr in drs){
               newTable.ImportRow(dr);
               } newTable.AcceptChanges();
               RepChild.DataSource =newTable;
               RepChild.DataBind();
               newTable.Clear();
           }
       }

    遇到的问题:

      在子控件RepProTypeChild的DataBound事件绑定中,刚开始验证了等于item类型的绑定。测试发现item只能绑定到单数的行。加入 了AlternatingItem才正常。经查询:

    ListItemType.Item:单数行

    AlternatingItem:双数行

    两个必须同时使用才能验证Repeater 所有的行数据绑定

    表克隆:以前学过,但没用。昨天使用了一把,还是经验太少的缘故啊:

    newTable=proTypeTable.clone();在新表中将复制另外一个表的架构和约束

    DataRow[]不能作为数据源,必须要放到表中或其它类型的数组。

  • 相关阅读:
    Python中append和extend的区别
    python学习 day19
    python学习 day18
    QT下编写使用for循环动态添加刻选择时间类型的按钮(记录一下)
    QT mingw编译器下使用snap7库与西门子200smart-PLC(网口)通信实现代码
    看着挺胖的大胖猫
    QT添加软键盘后,QDialog设置模态后软键盘没响应解决办法
    QT程序打包在别的电脑上运行提示“api-ms-win-crt-runtime-|1-1-0.dll"可能与您正在运行的Window版本不兼容。。。。。
    Qt使用WM_COPYDATA消息进行进程通信
    离线百度地图,QT添加按钮点击切换卫星地图和街道地图
  • 原文地址:https://www.cnblogs.com/fogwang/p/2666607.html
Copyright © 2011-2022 走看看