續——datalist嵌套方法二

前臺代碼

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="datalist嵌套.aspx.cs" Inherits="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></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
   
        <asp:DataList ID="DataList1" runat="server"
            onitemdatabound="DataList1_ItemDataBound" Width="175px">
            <ItemTemplate>
                fathertype:
                <asp:Label ID="fathertypeLabel" runat="server"
                    Text='<%# Eval("fathertype") %>' />
                <br />
                <asp:DataList ID="DataList2" runat="server" Width="198px">
                    <ItemTemplate>
                        childtype:
                        <asp:Label ID="childtypeLabel" runat="server" Text='<%# Eval("childtype") %>' />
                        <br />
                        <br />
                    </ItemTemplate>
                </asp:DataList>
                <br />
            </ItemTemplate>
        </asp:DataList>
        <asp:SqlDataSource ID="SqlDataSource1" runat="server"
            ConnectionString="<%$ ConnectionStrings:ConnectionString %>"
            SelectCommand="SELECT distinct [fathertype] FROM [goodsclass]">
        </asp:SqlDataSource>
        <asp:SqlDataSource ID="SqlDataSource2" runat="server"
            ConnectionString="<%$ ConnectionStrings:ConnectionString %>"
            SelectCommand="SELECT [childtype] FROM [goodsclass] WHERE ([fathertype] = @fathertype)">
            <SelectParameters>
                <asp:SessionParameter Name="fathertype" SessionField="father" Type="String" />
            </SelectParameters>
        </asp:SqlDataSource>
   
    </div>
    </form>
</body>
</html>

 

後臺代碼:

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

public partial class datalist嵌套 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            this.DataList1.DataSource = this.SqlDataSource1;
            this.DataList1.DataBind();
        }

    }
    protected void DataList1_ItemDataBound(object sender, DataListItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.Item ||
            e.Item.ItemType == ListItemType.AlternatingItem)
        {
                  System.Data.DataRowView drv =
                (System.Data.DataRowView)(e.Item.DataItem);
            string quantity = drv.Row["fathertype"].ToString();
            Session["father"] = quantity;
            DataList dataList2 = (DataList)e.Item.FindControl("DataList2");
            dataList2.DataSource = this.SqlDataSource2;
            dataList2.DataBind();                                              //同樣需要動態綁定

           }
    }
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章