asp.net中Ext.Net_自定义 TreePanel 控件实现代码

标记 <ext:ResourcePlaceHolder ID="ResourcePlaceHolder1" runat="server" Mode="ScriptFiles" />,Mode="ScriptFiles" 是为了 MyTreePanel 呈现后,保证页面在正确的时机加载,并执行 MyCustomControl.MyTreePanel.init 方法,初始化其 tr 属性,以便在客户端操作该控件;

public const string SCOPE = "MyCustomControl.MyTreePanel"; 语句,配合脚本的 Ext.ns("MyCustomControl"); 以及 MyCustomControl.MyTreePanel = { … },在脚本创建命名空间和 MyTreePanel 类。

这种方式在很大程度上提高页面性能,便于日后维护该控件。

先看看封装后,在页面中引用自定义控件,代码如下:

 代码如下 复制代码

<%@ Page Language="C#" %> 

<%@ Register Assembly="Ext.Net" Namespace="Ext.Net" TagPrefix="ext" %>

<%@ Register Assembly="ExtNetTreePanleCustomControl" Namespace="ExtNetTreePanleCustomControl.MyCustomControl"

    TagPrefix="MyControl" %>

<!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>

    <ext:ResourcePlaceHolder ID="ResourcePlaceHolder1" runat="server" Mode="ScriptFiles" />

 

    <script src="resources/myTreePanel.js" type="text/javascript"></script>

 

</head>

<body>

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

    <ext:ResourceManager ID="ResourceManager1" runat="server" InitScriptMode="Linked"

        RemoveViewState="true" IDMode="Explicit" />

    <MyControl:MyTreePanel ID="MyTreePanel1" runat="server" />

    </form>

</body>

</html>

 

其中,"<MyControl:MyTreePanel ID="MyTreePanel1" runat="server" />" 是自定义的 TreePanel 控件。

自定义控件的分部类 MyTreePanel,文件名分别为 MyTreePanelLogic.cs 和 MyTreePanelUI.cs

 代码如下 复制代码

using System;

using System.Collections.Generic;

using System.Linq;

 using Ext.Net.Utilities;

using Ext.Net;

 namespace ExtNetTreePanleCustomControl.MyCustomControl

{

    [DirectMethodProxyID(IDMode = DirectMethodProxyIDMode.None)]

    public partial class MyTreePanel

    {

        public const string SCOPE = "MyCustomControl.MyTreePanel";

         private void InitLogic()

        {

            this.Listeners.Render.Fn = MyTreePanel.SCOPE + ".init";

            this.Listeners.Render.Scope = MyTreePanel.SCOPE;

            this.Listeners.CheckChange.Handler = MyTreePanel.SCOPE + ".SelectParentChildNodes(node,checked);";

             Ext.Net.Button button = new Ext.Net.Button();

            button.ID = "btnGet";

            button.Text = "获得勾选";

            button.Listeners.Click.Handler = MyTreePanel.SCOPE + ".getSelectedNode(#{" + this.ID + "})";

            this.Buttons.Add(button);

            button = new Ext.Net.Button();

            button.ID = "btnClear";

            button.Text = "清除勾选";

            button.Listeners.Click.Handler = MyTreePanel.SCOPE + ".clearSelectedNode(#{" + this.ID + "})";

            this.Buttons.Add(button);

        }

    }

}

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Linq;

using System.Xml.Serialization;

using Ext.Net;

using Newtonsoft.Json;

 

namespace ExtNetTreePanleCustomControl.MyCustomControl

{

    public partial class MyTreePanel : TreePanel

    {

        public MyTreePanel()

        {

            this.ID = "MyTreePanel1";

            this.Title = "MyTreePanel";

            this.Width = System.Web.UI.WebControls.Unit.Pixel(300);

            this.Height = System.Web.UI.WebControls.Unit.Pixel(400);

            this.UseArrows = true;

            this.AutoScroll = true;

            this.Animate = true;

            this.EnableDD = true;

            this.ContainerScroll = true;

            this.RootVisible = false;

 

            this.LoadMask.ShowMask = true;

 

            this.SelectionModel.Add(new Ext.Net.DefaultSelectionModel());

 

            this.BuildTree();

            this.InitLogic();

        }

 

        private void BuildTree()

        {

            Ext.Net.TreeNode root = new Ext.Net.TreeNode("Composers");

            root.Expanded = true;

            this.Root.Add(root);

 

 

            List<Composer> composers = MyData.GetData();

 

            foreach (Composer composer in composers)

            {

                Ext.Net.TreeNode composerNode = new Ext.Net.TreeNode(composer.Name, Icon.UserGray);

                composerNode.Checked = Ext.Net.ThreeStateBool.False;

                composerNode.Leaf = false;

                composerNode.Icon = Ext.Net.Icon.Folder;

                root.Nodes.Add(composerNode);

 

                foreach (Composition composition in composer.Compositions)

                {

                    Ext.Net.TreeNode compositionNode = new Ext.Net.TreeNode(composition.Type.ToString());

                    compositionNode.Checked = Ext.Net.ThreeStateBool.False;

                    compositionNode.Leaf = false;

                    compositionNode.Icon = Ext.Net.Icon.Folder;

                    composerNode.Nodes.Add(compositionNode);

 

                    foreach (Piece piece in composition.Pieces)

                    {

                        Ext.Net.TreeNode pieceNode = new Ext.Net.TreeNode(piece.Title, Icon.Music);

                        pieceNode.Checked = Ext.Net.ThreeStateBool.False;

                        pieceNode.Leaf = true;

                        compositionNode.Nodes.Add(pieceNode);

                    }

                }

            }

        }

    }

}

数据

 代码如下 复制代码

using System;

using System.Collections;

using System.Collections.Generic;

using System.Globalization;

using System.Web;

using System.Xml;

using Ext.Net;

 

namespace ExtNetTreePanleCustomControl

{

    public class Composer

    {

        public Composer(string name) { this.Name = name; }

        public string Name { get; set; }

 

        private List<Composition> compositions;

        public List<Composition> Compositions

        {

            get

            {

                if (this.compositions == null)

                {

                    this.compositions = new List<Composition>();

                }

                return this.compositions;

            }

        }

    }

 

    public class Composition

    {

        public Composition() { }

 

        public Composition(CompositionType type)

        {

            this.Type = type;

        }

 

        public CompositionType Type { get; set; }

 

        private List<Piece> pieces;

        public List<Piece> Pieces

        {

            get

            {

                if (this.pieces == null)

                {

                    this.pieces = new List<Piece>();

                }

                return this.pieces;

            }

        }

    }

 

    public class Piece

    {

        public Piece() { }

 

        public Piece(string title)

        {

            this.Title = title;

        }

 

        public string Title { get; set; }

    }

 

    public enum CompositionType

    {

        Concertos,

        Quartets,

        Sonatas,

        Symphonies

    }

 

    public class MyData

    {

        public static List<Composer> GetData()

        {

            Composer beethoven = new Composer("Beethoven");

 

            Composition beethovenConcertos = new Composition(CompositionType.Concertos);

            Composition beethovenQuartets = new Composition(CompositionType.Quartets);

            Composition beethovenSonatas = new Composition(CompositionType.Sonatas);

            Composition beethovenSymphonies = new Composition(CompositionType.Symphonies);

 

            beethovenConcertos.Pieces.AddRange(new List<Piece> {

            new Piece{ Title = "No. 1 - C" },

            new Piece{ Title = "No. 2 - B-Flat Major" },

            new Piece{ Title = "No. 3 - C Minor" },

            new Piece{ Title = "No. 4 - G Major" },

            new Piece{ Title = "No. 5 - E-Flat Major" }

        });

 

            beethovenQuartets.Pieces.AddRange(new List<Piece> {

            new Piece{ Title = "Six String Quartets" },

            new Piece{ Title = "Three String Quartets" },

            new Piece{ Title = "Grosse Fugue for String Quartets" }

        });

 

            beethovenSonatas.Pieces.AddRange(new List<Piece> {

            new Piece{ Title = "Sonata in A Minor" },

            new Piece{ Title = "sonata in F Major" }

        });

 

            beethovenSymphonies.Pieces.AddRange(new List<Piece> {

            new Piece{ Title = "No. 1 - C Major" },

            new Piece{ Title = "No. 2 - D Major" },

            new Piece{ Title = "No. 3 - E-Flat Major" },

            new Piece{ Title = "No. 4 - B-Flat Major" },

            new Piece{ Title = "No. 5 - C Minor" },

            new Piece{ Title = "No. 6 - F Major" },

            new Piece{ Title = "No. 7 - A Major" },

            new Piece{ Title = "No. 8 - F Major" },

            new Piece{ Title = "No. 9 - D Minor" }

        });

 

            beethoven.Compositions.AddRange(new List<Composition>{

            beethovenConcertos,

            beethovenQuartets,

            beethovenSonatas,

            beethovenSymphonies

        });

 

 

            Composer brahms = new Composer("Brahms");

 

            Composition brahmsConcertos = new Composition(CompositionType.Concertos);

            Composition brahmsQuartets = new Composition(CompositionType.Quartets);

            Composition brahmsSonatas = new Composition(CompositionType.Sonatas);

            Composition brahmsSymphonies = new Composition(CompositionType.Symphonies);

 

            brahmsConcertos.Pieces.AddRange(new List<Piece> {

            new Piece{ Title = "Violin Concerto" },

            new Piece{ Title = "Double Concerto - A Minor" },

            new Piece{ Title = "Piano Concerto No. 1 - D Minor" },

            new Piece{ Title = "Piano Concerto No. 2 - B-Flat Major" }

        });

 

            brahmsQuartets.Pieces.AddRange(new List<Piece> {

            new Piece{ Title = "Piano Quartet No. 1 - G Minor" },

            new Piece{ Title = "Piano Quartet No. 2 - A Major" },

            new Piece{ Title = "Piano Quartet No. 3 - C Minor" },

            new Piece{ Title = "Piano Quartet No. 3 - B-Flat Minor" }

        });

 

            brahmsSonatas.Pieces.AddRange(new List<Piece> {

            new Piece{ Title = "Two Sonatas for Clarinet - F Minor" },

            new Piece{ Title = "Two Sonatas for Clarinet - E-Flat Major" }

        });

 

            brahmsSymphonies.Pieces.AddRange(new List<Piece> {

            new Piece{ Title = "No. 1 - C Minor" },

            new Piece{ Title = "No. 2 - D Minor" },

            new Piece{ Title = "No. 3 - F Major" },

            new Piece{ Title = "No. 4 - E Minor" }

        });

 

            brahms.Compositions.AddRange(new List<Composition>{

            brahmsConcertos,

            brahmsQuartets,

            brahmsSonatas,

            brahmsSymphonies

        });

 

 

            Composer mozart = new Composer("Mozart");

 

            Composition mozartConcertos = new Composition(CompositionType.Concertos);

 

            mozartConcertos.Pieces.AddRange(new List<Piece> {

            new Piece{ Title = "Piano Concerto No. 12" },

            new Piece{ Title = "Piano Concerto No. 17" },

            new Piece{ Title = "Clarinet Concerto" },

            new Piece{ Title = "Violin Concerto No. 5" },

            new Piece{ Title = "Violin Concerto No. 4" }

        });

 

            mozart.Compositions.Add(mozartConcertos);

 

            return new List<Composer> { beethoven, brahms, mozart };

        }

    }

}

外部脚本文件 myTreePanel.js

 代码如下 复制代码

Ext.ns("MyCustomControl");

 

// ------------------MyTreePanel----------------------------------

MyCustomControl.MyTreePanel = {

    tr: null,

    init: function(tree) {

        this.tr = tree;

    },

    SelectParentChildNodes: function(node, state) {

        var tree = node.getOwnerTree();

        tree.suspendEvents();

        if (node.parentNode != null) {

            // 勾选该节点所有子节点

            node.cascade(function(node) {

                node.attributes.checked = state;

                node.ui.toggleCheck(state);

                return true;

            });

            // 勾选该节点父节点

            var pNode = node.parentNode;

            while (pNode != null) {

                if (state) { //如果选中状态无所谓

                    pNode.ui.toggleCheck(state);

                    pNode.attributes.checked = state;

                    pNode = pNode.parentNode;

                }

                else { //如果未选中状态,需要查看该节点是否所有子节点都未选中

                    var chk = false;

                    pNode.eachChild(function(child) {

                        if (child.attributes.checked || child.getUI().isChecked())

                            chk = true;

                    });

                    pNode.ui.toggleCheck(chk);

                    pNode.attributes.checked = chk;

                    pNode = pNode.parentNode;

                }

            }

        }

        tree.resumeEvents();

    },

    getSelectedNode: function(tree) {

        var msg = [];

        var selNodes = tree.getChecked();

 

        Ext.each(selNodes, function(node) {

            msg.push(node.text);

        });

 

        Ext.Msg.show({

            title: "勾选节点",

            msg: msg.join(','),

            icon: Ext.Msg.INFO,

            minWidth: 200,

            buttons: Ext.Msg.OK

        });

    },

    clearSelectedNode: function(tree) {

        tree.clearChecked();

    }

};

运行结果如下

时间: 2024-10-29 07:36:08

asp.net中Ext.Net_自定义 TreePanel 控件实现代码的相关文章

timer-DEV 第三方控件中的使用中自定义用户控件里面的Timer 控件的使用

问题描述 DEV 第三方控件中的使用中自定义用户控件里面的Timer 控件的使用 用户控件中使用的代码如下:/asp:ScriptManager /asp:Timer /asp:Literal /asp:UpdatePanel 为什么到事件刷新的时候会报 " Sys.WebForms.PageRequestManagerParserErrorException: 未能分析从服务器收到的消息." 这个错误呢?请指教一下 解决方案 asp不会,jsp会一点,祝你好运!

asp.net中的文件上传控件能否打开一个固定文件夹然后再选择文件

问题描述 asp.net中的文件上传控件能否打开一个固定文件夹然后再选择文件asp.net中的文件上传控件能否打开一个固定文件夹然后再选择文件我现在要上传文件,但是在点击浏览的时候我想要打开一个指定的目录,然后在这个目录中选择文件..用什么方法能够实现呢?请各位大侠帮忙呀!!!!!!!!!??????????????????????????谢谢各位!!!!!!!! 解决方案 解决方案二:属于客户端程序,安全考虑的话怕不可行解决方案三:可以,将所选目录文件列出就行了,用常用的IO操作就能实现CSD

详细阐述利用ASP.NET 2.0创建自定义Web控件开发说明

asp.net|web|创建|控件|控件开发 简介 从使用基本的文本编辑器到创作标记页面,Web 开发已经经历了一个漫长的过程.目前,集成开发环境 (IDE) 为开发过程中的几乎每个方面都提供了图形化表示形式.此外,还实现各种说明性编程技术以提高效率并降低出现错误的几率.Visual Studio 2005 和 ASP.NET 2.0 中的控件体系结构遵循了这些编程趋势,并且提供了可靠的.可扩展的环境,该环境设计为使开发人员可以创建能够以说明方式配置的控件. 此外,ASP.NET 中新的自适应呈

利用 ASP.NET 2.0 创建自定义 Web 控件

asp.net|web|创建|控件 Jayesh Patel.Bryan Acker.Robert McGovernInfusion Development适用于:Microsoft ASP.NET 2.0Microsoft Visual Studio 2005 摘要:ASP.NET 2.0 中新的自适应呈现模型为控件编写人员提供了很多新的选项.本文展示了这些选项如何使创建 ASP.NET 的自定义控件变得比以前更加容易. 本页内容 简介 自适应呈现模型 创建自定义服务器控件 TagKey 使用

利用ASP.NET 2.0创建自定义Web控件

asp.net|web|创建|控件 从使用基本的文本编辑器到创作标记页面,Web 开发已经经历了一个漫长的过程.目前,集成开发环境 (IDE) 为开发过程中的几乎每个方面都提供了图形化表示形式.此外,还实现各种说明性编程技术以提高效率并降低出现错误的几率.Visual Studio 2005 和 ASP.NET 2.0 中的控件体系结构遵循了这些编程趋势,并且提供了可靠的.可扩展的环境,该环境设计为使开发人员可以创建能够以说明方式配置的控件. 此外,ASP.NET 中新的自适应呈现模型减少了编写

asp.net-C#中 鼠标移动到某控件上的事件

问题描述 C#中 鼠标移动到某控件上的事件 想要实现 当我的鼠标移动到控件上的时候 可以显示一个小小的提示框 提示某信息 举个栗子的话 就是 在qq主面板上的时候 如果我把鼠标停留在 五角星上一会 然后就会显示"QQ空间" 这样 搜了很多鼠标事件·· 但是还是不知道这个小提示应该怎么写出来 辛苦了 (:з」∠)

Android开发中DatePicker日期与时间控件实例代码

一.简介 二.方法 最日常的使用方法了 日期控件DatePicker 时间控件TimePicker 月份从0开始 三.代码实例 效果图: 代码: fry.Activity01 package fry; import com.example.DatePicherDemo1.R; import android.app.Activity; import android.os.Bundle; import android.widget.DatePicker; import android.widget.

[转]利用ASP.NET 2.0创建自定义Web控件(2)

原址:http://hi.baidu.com/sjbh/blog/item/5a8298454403a321cffca39c.html   如何生成的? Render() 方法基本上控制着 WebControl 的整个输出.默认情况下,Render() 方法实际上会依次调用 RenderBeginTag().RenderContents() 以及 RenderEndTag().尽管在 ASP.NET 1.x 中调用结构并未变化,但由于该呈现模型,修改这些调用的影响却发生了变化. 您可以覆盖 Re

asp.net中数据库绑定到DataList控件操作方法

我现在要做这个页面,然后我用DataList控件显示题目,我想问如何把我数据库教程的题目绑定到DataList控件里面,然后让它显示考试题目? 解决方法1: datalist databind() 解决方法2: 查看MSDN上的详细说明资料 解决方法3: 在DataList的模板中用table表格,如:<asp教程:DataList ID="dlDetailedInfo" runat="server" OnItemDataBound="dlDetai