<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="DataList控件.aspx.cs" Inherits="WebApplication1.DataList控件" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>DataList嵌套绑定例子</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:DataList ID="DataList1" runat="server"
RepeatColumns="" RepeatDirection="Horizontal" OnItemDataBound="DataList1_ItemDataBound">
<ItemTemplate>
<div style="background-color:Green">
<asp:Label ID="Label1" runat="server" Text='<%#Eval("Sex")%>' Visible="false"></asp:Label>
<%#bool.Parse(Eval("Sex").ToString())==true?"男":"女" %>
</div>
<asp:DataList ID="DataList2" runat="server">
<ItemTemplate>
<%#Eval("RealName")%>
</ItemTemplate>
</asp:DataList>
</ItemTemplate>
</asp:DataList>
</div>
</form>
</body>
</html>

CS代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data; namespace WebApplication1
{
public partial class DataList控件 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
BindSex();
}
}
//绑定顶级项目
private void BindSex()
{
SqlConnection conn = new SqlConnection(@"server=Rose-PC\SQLEXPRESS;Database=User;User Id=sa;password=");
SqlCommand command = new SqlCommand("Select distinct Sex from UserInfo", conn);
SqlDataAdapter adapter = new SqlDataAdapter(command);
DataTable data = new DataTable();
adapter.Fill(data); DataList1.DataSource = data;
DataList1.DataBind();
}
//当绑定DataList1中的每一项时的处理方法
protected void DataList1_ItemDataBound(object sender, DataListItemEventArgs e)
{
//如果要绑定的项是交替项或者是普通项
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
//e.Item表示当前绑定的那行
//利用e.Item.FindControl("Label1")来找到那一行的id为“Label”的Label控件
Label lbSex = (Label)(e.Item.FindControl("Label1"));
//利用e.Item.FindControl("DataList2")来找到那一行的id为“DataList2”的DataList2控件
DataList dl2 = (DataList)(e.Item.FindControl("DataList2")); bool male = bool.Parse(lbSex.Text);
dl2.DataSource = GetDataTable(male);
dl2.DataBind();
}
} private DataTable GetDataTable(bool male)
{
SqlConnection conn = new SqlConnection(@"server=Rose-PC\SQLEXPRESS;Database=User;User Id=sa;password=");
SqlCommand command = new SqlCommand("Select top 3 RealName from UserInfo where Sex=@Sex order by ID", conn);
command.Parameters.AddWithValue("@Sex", male);
SqlDataAdapter adapter = new SqlDataAdapter(command);
DataTable data = new DataTable();
adapter.Fill(data);
return data;
} }
}

注意:上面的代码使用了两个DataList控件,第二个位于itemTemplate模板里,用于绑定当前数据,在第一个itemTemplate里还用到一个Label控件,这个是不可见的(visible=“false”) 这个不是为了显示数据,是为了保护第二个DataList控件要绑定显示数据的条件

DataList嵌套绑定例子的更多相关文章

  1. ASP.NET-DataList控件-DataList嵌套

    DataList是ASP.NET的数据控件之一,在使用时要对其进行数据绑定.但是使用过程中难免会出现需要根据已绑定表中的某列数据来作进一步的查询和显示,就需要使用DataList嵌套来解决此类问题. ...

  2. QFramework 使用指南 2020 (四):脚本生成(2)ViewController 与 ViewController 嵌套绑定

    在上一篇,我们学习了,脚本生成的基本使用. 在这一篇,我们试着深入,聊聊脚本生成给我们带来的便利. 脚本生成的便利 首先,我们要知道,在 Unity 的游戏世界中都是以 GameObject 为单位的 ...

  3. DataList与Repeater嵌套绑定

    <%@ Page Language="C#" AutoEventWireup="true" CodeFile="home.aspx.cs&quo ...

  4. ASP.NET DataList嵌套实现评论效果

    问题: Datalist1显示say这个表的数据 然后想在Datalist1中嵌套一个Datalist2用于显示对应的评论表2的 sayID对应表1的id,若表2中找不到对应sayId则在对应的Dat ...

  5. repeater三级嵌套绑定

    <asp:Repeater ID="rpt1" runat="server" onitemdatabound="rpt1_ItemDataBou ...

  6. [置顶] Datalist嵌套datalist,页面传值,加密,数据绑定

    <asp:DataList ID="dlMajor" runat="server" CssClass="dllist" OnItemD ...

  7. Python基本数据结构之字典嵌套(例子)

    北京城市地区之间的嵌套 # coding=gbk #Created on 2019/5/20 #@author: XiaoHu menu = { '北京': { '朝阳': { '国贸': { 'CI ...

  8. 关于Repeater嵌套绑定的问题

    前台代码: <div id="firstpane" class="menu_list">                <asp:Repeat ...

  9. Repeater嵌套绑定Repeater

    前台Html代码 <asp:Repeater runat="server" ID="rpList" OnItemDataBound="rpLis ...

随机推荐

  1. (转)Java通过axis调用WebService

    转自:http://blog.csdn.net/wanglha/article/details/49679825 转载地址:http://www.linuxidc.com/Linux/2015-06/ ...

  2. Linux学习之输入输出重定向

    转自:http://www.cnblogs.com/chengmo/archive/2010/10/20/1855805.html 多谢分享 在了解重定向之前,我们先来看看linux 的文件描述符. ...

  3. Gow工具

    一 Gow 是什么 Gow (Gnu On Windows) is the lightweight alternative to Cygwin. It uses a convenient NSIS i ...

  4. Oracle的TPCC测试,原来也是个作弊的东西...

    http://www.oaktable.net/content/sorted-hash-clusters-rip 根据Jonathan Lewis老先生的测试实例,发觉cluster 的sort功能, ...

  5. mysql 函数执行权限

    mysql> show grants for query_all@'115.236.1x0.x'; +---------------------------------------------- ...

  6. [2013.9.8网络首发]导入Android4.2源码里的Gallery2和Camera模块至Eclipse全过程

    [2013.9.8网络首发]导入Android4.2源码里的Gallery2和Camera模块至Eclipse全过程   google的android自带的apps写的是相当牛逼的,将其导入到ecli ...

  7. HDU 5919 Sequence II(可持久化线段树)

    [题目链接]http://acm.hdu.edu.cn/showproblem.php?pid=5919 [题目大意] 给出一个数列,每次查询数列中,区间非重元素的下标的中位数.查询操作强制在线. [ ...

  8. POJ2352:Stars

    题目 Stars Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 34016   Accepted: 14839 Descri ...

  9. SQLServer 2008的组成

    SQLServer 2008的组成: 1.主要数据库文件:有且只有一个,文件后缀为.mdf. 2.日志文件:至少有一个,文件后缀为.ldf. 3.次要数据库文件:任意个,文件后缀为.ndf.

  10. 九度OnlineJudge之1012:畅通工程

    题目描述: 某省调查城镇交通状况,得到现有城镇道路统计表,表中列出了每条道路直接连通的城镇.省政府“畅通工程”的目标是使全省任何两个城镇间都可以实现交通(但不一定有直接的道路相连,只要互相间接通过道路 ...