let it be
行到水穷处 坐看云起时

在进行程序开发进行数据绑定的时候,可能需要进行数据绑定控件的嵌套使用。而在数据绑定控件中,DataList控件是最好的显示嵌套主从关系的控件。即在一个DataList中嵌套一个子DataList,而子DataList的数据则是随主DataList的数据进行变化的。下面就开始介绍如何实现DataList控件的嵌套。

首先在页面中布置好两个DataList控件。并分别起名,dlsoutdlsinner

其次将页面对应的HTML代码按照如下代码设置:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="DataList_exam10.aspx.cs" Inherits="DataList_exam10" %>

<!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_exam10</title>

</head>

<body>

<form id="form1" runat="server">

<div>

<table id="Table3" cellSpacing="0" cellPadding="0" width="90%" align="center" border="0">

<tbody>

<tr>

<td vAlign="top">

<asp:datalist id="dlsout" runat="server" RepeatDirection="Horizontal" RepeatColumns="2"

Width="100%">

<ItemTemplate>

<table class="table02" id="Table13" cellSpacing="0" cellPadding="0" width="100%" align="center"

border="0">

<tr bgColor="#f5f5f5">

<td width="50%" height="30">

<%# DataBinder.Eval(Container.DataItem, "ZhuCYHM")%>

</td>

</tr>

</table>

<asp:datalist id="dlsinner" Width="100%" runat="server" RepeatColumns="1">

<ItemTemplate>

<table class="table02" id="Table14" cellSpacing="0" cellPadding="0" width="100%" align="center"

border="0">

<tr bgColor="#f5f5f5">

<td width="50%" height="30">

<div class="style10" align="left">标题: <a>

<%# DataBinder.Eval(Container.DataItem, "ZhuCYHM")%>

</a>

</div>

</td>

</tr>

</table>

</ItemTemplate>

</asp:datalist>

</ItemTemplate>

</asp:datalist>

</td>

</tr>

</tbody>

</table>

</div>

</form>

</body>

</html>

最后在页面对应的后台代码终实现数据的绑定,以及实现两个DataList控件的嵌套代码。在建立数据库连接之前将using System.Data.SqlClient;添加到头命名空间代码中去。在建立数据库连接之后将T_YongH表中的BianH,ZhuCYHM,XingM,XingB字段查询出来,并将ZhuCYHM字段作为dlsout控件的DataKeys,有了DataKeys就可以唯一的确定一条数据记录信息。并且为dlsout控件生成dlsout_ItemDataBound事件,这这个事件中主要实现将对应dlsout控件中的ZhuCYHM的记录信息显示在dlsinner控件中。

public partial class DataList_exam10 : System.Web.UI.Page

{

       //Page_Load中将查询的数据信息绑定在dlsout

    protected void Page_Load(object sender, EventArgs e)

    {

        //声明一个字符串

        string sConnectionString;

        //连接数据库字符串,连接到XBMIS数据库,用户名是sa

        sConnectionString = " Data Source=.;Initial Catalog= XBMIS;User ID=sa; ";

        //创建SqlConnection数据库连接对象

        SqlConnection Conn = new SqlConnection(sConnectionString);

        SqlDataAdapter Adapter = new SqlDataAdapter("SELECT BianH, ZhuCYHM, XingM, XingB FROM T_YongH", Conn);

        Conn.Open();

        DataSet ds = new DataSet();

        Adapter.Fill(ds, "yongh");

        dlsout.DataSource = ds.Tables["yongh"].DefaultView;

        dlsout.DataBind();

}

//获取dlsout控件中的ZhuCYHM字段,并将这个字段作为参数进行数据库查询,最后以

//DataSet的形式返回

    public DataSet GetTitleID(string au_id)

    {

        SqlConnection cn = new SqlConnection("Data Source=.;Initial Catalog= XBMIS;User ID=sa;");

        SqlDataAdapter da = new SqlDataAdapter("SELECT BianH, ZhuCYHM, XingM, XingB FROM T_YongH where ZhuCYHM = @yhm", cn);

        da.SelectCommand.Parameters.Add("@yhm", SqlDbType.VarChar, 11).Value = au_id;

        DataSet ds = new DataSet();

        cn.Open();

        da.Fill(ds);

        cn.Close();

        return ds;

}

//GetTitleID返回的数据集绑定到dlsinner控件上

    protected void dlsProductMenu_ItemDataBound(object sender, DataListItemEventArgs e)

    {

        if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)

        {

                 //创建dlsinner控件的对象dataList

            DataList dataList = (DataList)e.Item.FindControl("dlsinner");

            DataRowView rowv = (DataRowView)e.Item.DataItem;

string mainID = Convert.ToString(rowv["ZhuCYHM"]);

//获取从GetTitleID数据,参数是mainID

            DataSet ds = GetTitleID(mainID);

            if (ds != null)

            {

                try

                {

                    dataList.DataSource = ds;

                    dataList.DataBind();

                }

                catch (Exception ex)

                {

                    throw new Exception(ex.Message);

                }

            }

        }

    }

}

posted on 2007-08-15 19:18  流浪浪  阅读(662)  评论(1编辑  收藏  举报