欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

asp.net实现DataList与Repeater嵌套绑定的方法

程序员文章站 2024-02-12 14:31:22
本文实例讲述了asp.net实现datalist与repeater嵌套绑定的方法。分享给大家供大家参考,具体如下: <%@ page language="c...

本文实例讲述了asp.net实现datalist与repeater嵌套绑定的方法。分享给大家供大家参考,具体如下:

<%@ page language="c#" autoeventwireup="true" codefile="home.aspx.cs" inherits="home" %>
<body>
  <form id="form1" runat="server">
       <asp:datalist id="monitortypelist" runat="server" repeatcolumns="4" 
         onitemdatabound="monitortypelist_itemdatabound" repeatdirection="horizontal" itemstyle-verticalalign="top">
       <itemtemplate>
        <table class="conbox" width="186" border="0" cellpadding="0" cellspacing="1" style="margin-right:10px;">
         <tr>
          <th><a href="<%#eval("plugpath") %>"><%#eval("monitor_type_name") %></a></th>
         </tr>
         <asp:repeater id="monitorconfiglist" runat="server">
          <itemtemplate>
         <tr>
          <td><a href="<%#eval("plugpath") %>?monitor_id=<%#eval("monitor_id") %>"><%#eval("monitor_name") %></a></td>
         </tr>
          </itemtemplate>
         </asp:repeater>
        </table>
      </itemtemplate>
      </asp:datalist>
  </form>
</body>

home.aspx.cs

using system;
using system.collections;
using system.configuration;
using system.data;
using system.linq;
using system.web;
using system.web.security;
using system.web.ui;
using system.web.ui.htmlcontrols;
using system.web.ui.webcontrols;
using system.web.ui.webcontrols.webparts;
using system.xml.linq;
public partial class home : system.web.ui.page
{
  protected void page_load(object sender, eventargs e)
  {
    mycheck.isloginnonreturn();
    if (!ispostback)
    {
      bind_monitortypelist();
    }
  }
  protected void bind_monitortypelist()
  {
    string sql = "select monitor_type_id,monitor_type_name from monitor_type";
    dbconn conn = new dbconn();
    dataset ds = conn.dataset(sql,"monitor_type");
    monitortypelist.datasource = ds.tables[0];
    monitortypelist.databind();
    ds.dispose();
    conn.close();
  }
  protected void monitortypelist_itemdatabound(object sender, datalistitemeventargs e)
  {
    if (e.item.itemtype == listitemtype.item || e.item.itemtype == listitemtype.alternatingitem)
    {
      string monitor_type_id = ((datarowview)e.item.dataitem).row["monitor_type_id"].tostring();
      repeater monitorconfiglist = (repeater)e.item.findcontrol("monitorconfiglist");
      if (monitorconfiglist != null)
      {
        string sql = "select monitor_id,nonitor_name,plugpath from monitor where monitor_type_id=" + monitor_type_id;
        dbconn conn = new dbconn();
        dataset ds = conn.dataset(sql, "monitor");
        monitorconfiglist.datasource = ds.tables[0];
        monitorconfiglist.databind();
        ds.dispose();
        conn.close();
      }
    }
  }
}

更多关于asp.net相关内容感兴趣的读者可查看本站专题:《asp.net文件操作技巧汇总》、《asp.net ajax技巧总结专题》及《asp.net缓存操作技巧总结》。

希望本文所述对大家asp.net程序设计有所帮助。