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 ...
随机推荐
- 字典 -- 数据结构与算法的javascript描述 第七章
字典 字典是一种以键-值对形式存储数据的数据结构 最基本功能规划 add 添加数据到字典 remove 从字典中移除数据 get 从字典中取出数据 count 统计字典数据量 find 查找数据在字典 ...
- IIS7.0/7.5 MVC3 实现伪静态
routes.MapRoute( "Default", "{controller}/{action}.html/{id}&qu ...
- SQL Server 数据库DML触发器 【一】
今天学习SQL Server数据库中DML触发器(DDL触发器以后有时间继续学习). 当删除一条创建有触发器的表中的内容时,触发器执行SQL语句. 1.首相先创建一张表,表名称是 [Test] , 内 ...
- ORACLE查看当前连接用户的权限信息或者角色信息
关于当前用户的相关信息,可以通过如下语句找到: SQL> select * from all_objects where object_name like 'SESSION%'; OWNER O ...
- Control的Invoke和BeginInvoke详解
(一)Control的Invoke和BeginInvoke 我们要基于以下认识: (1)Control的Invoke和BeginInvoke与Delegate的Invoke和BeginInvoke是不 ...
- JS 操作日期
var myDate = new Date(); myDate.getYear(); //获取当前年份(2位) myDate.getFullYear(); //获取完整的年份(4位,1970-???? ...
- hdu 4034 Graph floyd
题目链接 给出一个有向图各个点之间的最短距离, 求出这个有向图最少有几条边, 如果无法构成图, 输出impossible. folyd跑一遍, 如果dp[i][j] == dp[i][k]+dp[k] ...
- python parse命令行参数
#!/usr/bin/env python import sys def main(argv): for arg in argv: print arg if __name__ == '__main__ ...
- Linux ---> 监控JVM工具
Linux ---> 监控JVM工具shkingshking 发布时间: 2013/10/10 01:27 阅读: 2642 收藏: 26 点赞: 1 评论: 0 JDK内置工具使用 jps(J ...
- Orz 终于有了自己的博客地址
新博客地址:http://www.wnjxyk.cn/