⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 items.aspx.cs

📁 详细介绍中小企业的网站编程,附有详细的注释,对需要制作网站的朋友有很大的帮助,有需要的朋友可下载,
💻 CS
字号:
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;


public partial class Items : System.Web.UI.Page
{

    protected void Page_Load()
    {
        if (!IsPostBack)
        {
            Panel1.Visible = true;
            Panel2.Visible = false;
            TreeView1.Nodes.Clear();
            List<Category> categories = Catalog.GetChildCategories(String.Empty); // get root level ones
            foreach (Category c in categories)
            {
                TreeNode newNode = new TreeNode(c.Title, c.Id);
                newNode.SelectAction = TreeNodeSelectAction.SelectExpand;
                TreeView1.Nodes.Add(newNode);
                populateCategoryNode(newNode);
            }
        }
    }

    // 递归方法。
    private void populateCategoryNode(TreeNode n)
    {
        List<Category> categories = Catalog.GetChildCategories(n.Value);

        if (categories.Count == 0) // base case: leaf category 
        {
            return;
        }
        else //  recursive case : expand to show sub-categories
        {
            foreach (Category c in categories)
            {
                TreeNode newNode = new TreeNode(c.Title, c.Id);
                newNode.SelectAction = TreeNodeSelectAction.SelectExpand;
                n.ChildNodes.Add(newNode);
                populateCategoryNode(newNode);
            }
        }
    }

    //当单击树状视图中相应的项时,重新绑定GridView控件。
    protected void TreeView1_SelectedNodeChanged(Object source, EventArgs e)
    {
        //隐藏字段,用于保存分类ID号。
        TopCatId.Text = TreeView1.SelectedNode.Value;
        //分类标题。
        TopCatTitle.Text = TreeView1.SelectedNode.Text;
        //获取子分类列表。
        List<Category> categories = Catalog.GetChildCategories(TreeView1.SelectedNode.Value);
        //如果没有子分类列表,则隐藏Panel1。
        if (categories.Count == 0) // leaf category clicked --> show items
        {
            Panel1.Visible = false;
            Panel2.Visible = true;
            GridView1.DataBind();
        }
        else //显示产品列表。
        {
            Panel1.Visible = true;
            Panel2.Visible = false;
            GridView2.DataBind();
        }
    }


    /*
     *  根据Visible属性的值显示或隐藏相应的分类。
     */
    protected void GridView1_RowCreated(object source, GridViewRowEventArgs e)
    {
        if (e.Row == null) return;

        if (e.Row.RowType == DataControlRowType.DataRow && e.Row.DataItem != null)
        {
            bool visible = (bool)DataBinder.Eval(e.Row.DataItem, "Visible");
            e.Row.Visible = visible;
        }
    }


    private TreeNode findNode(string value, TreeNodeCollection nodes)
    {
        foreach (TreeNode n in nodes)
        {
            if (n.Value == value)
            {
                return n;
            }
            else // search n's child nodes
            {
                TreeNode nChild = findNode(value, n.ChildNodes);
                if (nChild != null)
                    return nChild;
            }
        }
        return null;
    }


    /*
     * e.CommandArgument = categoryID of the category that's clicked
     */
    protected void LinkButton1_OnCommand(object sender, CommandEventArgs e)
    {
        // update TreeView
        string categoryId = (string)e.CommandArgument;
        TreeNode n = findNode(categoryId, TreeView1.Nodes);
        TopCatTitle.Text = n.Text;
        if (n != null)
        {
            while (n != null)
            {
                n.Expand();
                n = n.Parent;
            }
        }

        // update page by updating text of control label
        Label controlLabel = (Label)Panel1.FindControl("TopCatId");
        if (controlLabel != null)
        {
            controlLabel.Text = categoryId;
            GridView2.DataBind();

            List<Category> categories = Catalog.GetChildCategories(categoryId);
            if (categories.Count == 0) // leaf category --> hide 'categories' &  make 'items' visible
            {
                Panel2.Visible = true;
                Panel1.Visible = false;
                GridView1.DataBind();
            }

        }

    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -