DataList嵌套绑定例子
<%@ 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嵌套绑定例子的更多相关文章
- ASP.NET-DataList控件-DataList嵌套
DataList是ASP.NET的数据控件之一,在使用时要对其进行数据绑定.但是使用过程中难免会出现需要根据已绑定表中的某列数据来作进一步的查询和显示,就需要使用DataList嵌套来解决此类问题. ...
- QFramework 使用指南 2020 (四):脚本生成(2)ViewController 与 ViewController 嵌套绑定
在上一篇,我们学习了,脚本生成的基本使用. 在这一篇,我们试着深入,聊聊脚本生成给我们带来的便利. 脚本生成的便利 首先,我们要知道,在 Unity 的游戏世界中都是以 GameObject 为单位的 ...
- DataList与Repeater嵌套绑定
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="home.aspx.cs&quo ...
- ASP.NET DataList嵌套实现评论效果
问题: Datalist1显示say这个表的数据 然后想在Datalist1中嵌套一个Datalist2用于显示对应的评论表2的 sayID对应表1的id,若表2中找不到对应sayId则在对应的Dat ...
- repeater三级嵌套绑定
<asp:Repeater ID="rpt1" runat="server" onitemdatabound="rpt1_ItemDataBou ...
- [置顶] Datalist嵌套datalist,页面传值,加密,数据绑定
<asp:DataList ID="dlMajor" runat="server" CssClass="dllist" OnItemD ...
- Python基本数据结构之字典嵌套(例子)
北京城市地区之间的嵌套 # coding=gbk #Created on 2019/5/20 #@author: XiaoHu menu = { '北京': { '朝阳': { '国贸': { 'CI ...
- 关于Repeater嵌套绑定的问题
前台代码: <div id="firstpane" class="menu_list"> <asp:Repeat ...
- Repeater嵌套绑定Repeater
前台Html代码 <asp:Repeater runat="server" ID="rpList" OnItemDataBound="rpLis ...
随机推荐
- 微信开发-Jssdk调用分享实例
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.IO ...
- 【转】iOS隐藏导航条1px的底部横线
默认情况下会有这条线 第一种方法: 1 2 3 4 5 6 UINavigationBar *navigationBar = self.navigationController.navigationB ...
- poj3162 Walking Race
题目大意:给一个树形图n个点(n-1条边),XXX要练习竞走,每次选定一个点k作为开始点,每次走从k开始能走的最长的一条路径(不要重边).要求出最长的连续的这样的k,假设连续有kx个,前提:这样kx条 ...
- windows8 安装IIS 和 添加网站(转)
Internet Information Services(IIS,互联网信息服务),是由微软公司提供的基于运行Microsoft Windows的互联网基本服务.最初是Windows NT版本的可选 ...
- 风行一时瀑布流网页布局,实现无限加载(jquery)
今天跟大家分享一个瀑布流网页布局,先跟大家分析下实现的思路吧 主要思路:一.根据屏幕宽度和单个浮动层的宽度来确定浮动层列数 var $boxs = $("#main>div" ...
- hdu 3518 Boring counting 后缀数组
题目链接 根据height数组的性质分组计算. #include <iostream> #include <vector> #include <cstdio> #i ...
- github/python/ show me the code 25题(二)
第 0014 题: 纯文本文件 student.txt为学生信息, 里面的内容(包括花括号)如下所示: { "1":["张三",150,120,100], &q ...
- 五年26个版本:Linux系统内核全程回顾
Phoronix.com今天将他们对Linux系统的研究发挥到了极致:从2005年年中的2.6.12,到正在开发中的2.6.37,五年多来的26个Linux内核版本来了个“群英荟萃”! 完成如此庞大规 ...
- Storm博客收集
http://wbj0110.iteye.com/category/292875 http://blog.csdn.net/hguisu/article/details/8454368?reload ...
- RedisTemplate.htm
http://docs.spring.io/spring-data/redis/docs/current/api/org/springframework/data/redis/core/RedisTe ...