当前位置:首页 > 日志>asp.net二级分类,datalist嵌套datalist

asp.net二级分类,datalist嵌套datalist

】 浏览:2248 添加日期:2010-10-15 22:16:42 

网上看了别人写的太复杂,我初学,看不懂,短短几句代码便可实现,不知为何他人写那么长,最可怜的事是都是转载,千篇一律。

下面我写的,相信BUG很多,望指点

数据库:

表名: proClass

id int,---主键
className varchar ,--类名
classUpper int ,--类的上一级 0为最大
)

代码:

        [asp:DataList ID="dlParentClass" runat="server" onitemdatabound="dlParentClass_ItemDataBound"]
            [ItemTemplate]
                [a href="Product.aspx?classId=[%# DataBinder.Eval(Container.DataItem,"id") %]"]
                [%# DataBinder.Eval(Container.DataItem,"className") %][/a]
                [asp:DataList ID="dlChildClass" runat="server" ]
                    [ItemTemplate]
                           ]
                        [a href="Product.aspx?classId=[%# DataBinder.Eval(Container.DataItem,"id") %]"]
                        [%# DataBinder.Eval(Container.DataItem,"className") %][/a]
                    [/ItemTemplate]
                [/asp:DataList]
            [/ItemTemplate]
            [/asp:DataList]

CS:

using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;

public partial class MyControll_product : System.Web.UI.UserControl
{
    DB DBManager = new DB();


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

    //获取大类
    private void getProClass()
    {
        DataSet ds = new DataSet();
        string strsql = "select * from proClass where classUpper=0";
        ds = DBManager.getDs(strsql, "UpClass");
        dlParentClass.DataSource = ds;
        dlParentClass.DataBind();
    }

    //外面的DataList的数据绑定事件
    protected void dlParentClass_ItemDataBound(object sender, DataListItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
        {
            DataList dl = (DataList)e.Item.FindControl("dlChildClass");
            DataRowView drv = (DataRowView)e.Item.DataItem;
            int parentId = Convert.ToInt32(drv["id"]);
            dl.DataSource = DBManager.getDs("select * from proClass where classUpper!=0 and classUpper=" + parentId, "LoClass");
            dl.DataBind();
        }
    }
}

〖C#.NET〗Tags:

  • 吴都君
  • 计算机及应用
  • 334194041
  • wudoujun@mail.china.com
  • wudoujun@163.com