asp.net代码练习 work032 DataList嵌套绑定示例

258 篇文章 2 订阅

webform1.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="work032.WebForm1" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <title>嵌套绑定的示例</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:DataList ID="DataList1" runat="server" RepeatColumns="2" RepeatDirection="Horizontal" OnItemDataBound="DataList1_ItemDataBound">
            <ItemTemplate>
                <div style="background-color:green;width:150px;">
                    <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("real_name") %>
                    </ItemTemplate>
                </asp:DataList>
            </ItemTemplate>
        </asp:DataList>
    </div>
    </form>
</body>
</html>

webform1.aspx.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace work032
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindSex();    
            }
        }

        private void BindSex()
        {
            System.Data.SqlClient.SqlConnectionStringBuilder bu = new System.Data.SqlClient.SqlConnectionStringBuilder();
            bu.DataSource = "(local)";
            bu.InitialCatalog = "test";
            bu.UserID = "sa";
            bu.Password = "123456";

            string conString = bu.ConnectionString;
            System.Data.SqlClient.SqlConnection con = new System.Data.SqlClient.SqlConnection(conString);
            con.Open();

            string sql = "select distinct sex from userinfo";
            System.Data.SqlClient.SqlDataAdapter adapter = new System.Data.SqlClient.SqlDataAdapter(sql, con);
            System.Data.DataTable dt = new System.Data.DataTable();
            adapter.Fill(dt);

            DataList1.DataSource = dt;
            DataList1.DataBind();


        }

        private System.Data.DataTable GetDataTable(bool male)
        {
            System.Data.SqlClient.SqlConnectionStringBuilder bu = new System.Data.SqlClient.SqlConnectionStringBuilder();
            bu.DataSource = "(local)";
            bu.InitialCatalog = "test";
            bu.UserID = "sa";
            bu.Password = "123456";

            string conString = bu.ConnectionString;
            System.Data.SqlClient.SqlConnection con = new System.Data.SqlClient.SqlConnection(conString);
            con.Open();

            string sql = "select real_name from userinfo where sex=@sex order by user_id asc";
            System.Data.SqlClient.SqlDataAdapter adapter = new System.Data.SqlClient.SqlDataAdapter(sql, con);
            adapter.SelectCommand.Parameters.AddWithValue("@sex",male);
            System.Data.DataTable dt = new System.Data.DataTable();
            adapter.Fill(dt);

            return dt;
        }

        protected void DataList1_ItemDataBound(object sender, DataListItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            { 
                Label lbSex = (e.Item.FindControl("Label1")) as Label;
                DataList list2 = (DataList)(e.Item.FindControl("DataList2"));

                bool male = bool.Parse(lbSex.Text);
                list2.DataSource = GetDataTable(male);
                list2.DataBind();

            }
        }
    }
}

sql

create database test;

create table UserInfo(
	user_id int identity(1,1) not null,
	user_name varchar(20) not null unique,
	real_name nvarchar(8) not null,
	age tinyint not null,
	sex bit not null,
	mobile varchar(14),
	phone varchar(14),
	email varchar(50) not null,
	primary key(user_id) 
);

insert into UserInfo values('zhangfei','张飞',36,1,'13011110001','8845996','zf@qq.com');
insert into UserInfo values('guanyu','关羽',38,1,'13011110002','8845995','gy@qq.com');
insert into UserInfo values('liubei','刘备',42,1,'13011110003','8845994','lb@qq.com');
insert into UserInfo values('zhaoyun','赵云',32,1,'13011110004','8845993','zy@qq.com');
insert into UserInfo values('huangzhong','黄忠',50,1,'13011110005','8845992','hz@qq.com');
insert into UserInfo values('caocao','曹操',48,1,'13011110006','8845991','cc@qq.com');
insert into UserInfo values('sunquan','孙权',33,1,'13011110007','8845990','sq@qq.com');
insert into UserInfo values('diaochan','貂蝉',20,0,'13011110008','8845880','dc@qq.com');
insert into UserInfo values('daqiao','大乔',21,0,'13011110009','8845881','dq@qq.com');
insert into UserInfo values('xiaoqiao','小乔',20,0,'13011110010','8845882','xq@qq.com');

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

虾米大王

有你的支持,我会更有动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值