smhy8187

 

datalist--checkbox

http://community.csdn.net/Expert/topic/5566/5566508.xml?temp=.1127741

<%@ Page Language="C#" %>
<%@ Import Namespace="System.Data" %>

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

<script runat="server">

    void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack) {
            LoadProductData();
        }
    }

    void dlstProCat_ItemDataBound(object sender, DataListItemEventArgs e)
    {       
        DataRowView drv = e.Item.DataItem as DataRowView;
        if(drv == null) return;

        DataRow dr = drv.Row;
        DataList dlstPro = e.Item.FindControl("dlstPro") as DataList;
        DataView dvPro = dr.Table.ChildRelations["ProCatShip"].ChildTable.DefaultView;
        dvPro.RowFilter = "CategoryId = " + dr["CategoryID"].ToString();
        dlstPro.DataSource = dvPro;
        dlstPro.DataBind();
    }

    void LoadProductData()
    {
        DataSet ds = CreateProductCategoryDataSet();
        dlstProCat.DataSource = ds.Tables["Categories"];
        dlstProCat.DataBind();
    }

    #region sample data
   
    static DataSet CreateProductCategoryDataSet()
    {
        DataSet ds = new DataSet("ProductCategorySet");
        DataTable dtPro = CreateProductTable();
        DataTable dtCat = CreateCategoryTable();
        ds.Tables.Add(dtPro);
        ds.Tables.Add(dtCat);
        ds.Relations.Add("ProCatShip", dtCat.Columns["CategoryID"], dtPro.Columns["CategoryID"]);

        return ds;
    }

    static DataTable CreateProductTable()
    {
        DataTable tbl = new DataTable("Products");

        tbl.Columns.Add("ProductID", typeof(int));
        tbl.Columns.Add("ProductName", typeof(string));
        tbl.Columns.Add("CategoryID", typeof(int));
        DataRow row = tbl.NewRow();
        row[0] = 1;
        row[1] = "Chai";
        row[2] = 1;
        tbl.Rows.Add(row);

        row = tbl.NewRow();
        row[0] = 2;
        row[1] = "Chang";
        row[2] = 1;
        tbl.Rows.Add(row);

        row = tbl.NewRow();
        row[0] = 3;
        row[1] = "Aniseed Syrup";
        row[2] = 2;
        tbl.Rows.Add(row);

        row = tbl.NewRow();
        row[0] = 4;
        row[1] = "Chef Anton's Cajun Seasoning";
        row[2] = 2;
        tbl.Rows.Add(row);

        row = tbl.NewRow();
        row[0] = 5;
        row[1] = "Chef Anton's Gumbo Mix";
        row[2] = 2;
        tbl.Rows.Add(row);

        row = tbl.NewRow();
        row[0] = 47;
        row[1] = "Zaanse koeken";
        row[2] = 3;
        tbl.Rows.Add(row);

        row = tbl.NewRow();
        row[0] = 48;
        row[1] = "Chocolade";
        row[2] = 3;
        tbl.Rows.Add(row);

        row = tbl.NewRow();
        row[0] = 49;
        row[1] = "Maxilaku";
        row[2] = 3;
        tbl.Rows.Add(row);

        return tbl;
    }

    public static DataTable CreateCategoryTable()
    {
        DataTable tbl = new DataTable("Categories");

        tbl.Columns.Add("CategoryID", typeof(int));
        tbl.Columns.Add("CategoryName", typeof(string));
        DataRow row = tbl.NewRow();
        row[0] = 1;
        row[1] = "Beverages";
        tbl.Rows.Add(row);

        row = tbl.NewRow();
        row[0] = 2;
        row[1] = "Condiments";
        tbl.Rows.Add(row);

        row = tbl.NewRow();
        row[0] = 3;
        row[1] = "Confections";
        tbl.Rows.Add(row);

        return tbl;
    }

    #endregion
  
</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Nested DataList</title>
    <script type="text/javascript">
    function chkCatPro_Clicked(sender, suredId1, suredId2)
    {
        var cnt = sender.parentElement.parentElement.parentElement;
        var nptArr = cnt.getElementsByTagName("input");
        for(var i = 0; i < nptArr.length; i++) {
            if(nptArr[i].type == 'checkbox' &&
                (nptArr[i].id.indexOf(suredId1) > -1 || nptArr[i].id.indexOf(suredId2) > -1)) {
                nptArr[i].checked = sender.checked;
            }
        }
    }
   
    function chkCat_Clicked(sender, suredId)
    {
        var cnt = sender.parentElement;
        var nptArr = cnt.getElementsByTagName("input");
        for(var i = 0; i < nptArr.length; i++) {
            if(nptArr[i].type == 'checkbox' && nptArr[i].id.indexOf(suredId) > -1) {
                nptArr[i].checked = sender.checked;
            }
        }
    }
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:DataList ID="dlstProCat" runat="server" OnItemDataBound="dlstProCat_ItemDataBound">
            <HeaderTemplate>
                <asp:CheckBox ID="chkCatPro" onclick="chkCatPro_Clicked(this, 'chkCat', 'chkPro')" runat="server" />                   
            </HeaderTemplate>
            <ItemTemplate>               
                <asp:CheckBox ID="chkCat" onclick="chkCat_Clicked(this, 'chkPro')" runat="server" />                   
                    <asp:Label ID="Label1" runat="server" Text='<%# Eval("CategoryName") %>'></asp:Label>
                    <div style="padding-left:20px;">
                    <asp:DataList ID="dlstPro" runat="server" >
                        <ItemTemplate>
                            <asp:CheckBox ID="chkPro" runat="server" />
                            <asp:Label ID="Label1" runat="server" Text='<%# Eval("ProductName") %>'></asp:Label>
                        </ItemTemplate>
                    </asp:DataList>
                    </div>
            </ItemTemplate>
        </asp:DataList>
       
    </div>
    </form>
</body>
</html>

posted on 2007-07-17 23:45  new2008  阅读(196)  评论(0编辑  收藏  举报

导航