佳木斯湛栽影视文化发展公司

主頁(yè) > 知識(shí)庫(kù) > asp.net使用DataGridTree實(shí)現(xiàn)下拉樹(shù)的方法

asp.net使用DataGridTree實(shí)現(xiàn)下拉樹(shù)的方法

熱門(mén)標(biāo)簽:人工智能 科大訊飛語(yǔ)音識(shí)別系統(tǒng) 電銷(xiāo)業(yè)務(wù) 客戶(hù)服務(wù) 國(guó)美全國(guó)運(yùn)營(yíng)中心 網(wǎng)站排名優(yōu)化 電商新玩法 百度AI接口

本文實(shí)例講述了asp.net使用DataGridTree實(shí)現(xiàn)下拉樹(shù)的方法。分享給大家供大家參考。具體實(shí)現(xiàn)方法如下:

下拉樹(shù)實(shí)現(xiàn)原理:輸出json到客戶(hù)端,客戶(hù)端實(shí)現(xiàn)動(dòng)態(tài)加載,中間不會(huì)和服務(wù)端交互。數(shù)據(jù)量支持上經(jīng)測(cè)試幾千還是很快的。本下拉樹(shù)控件是用c#+js樹(shù)實(shí)現(xiàn)。

2.c# 計(jì)算器 計(jì)算字符串?dāng)?shù)學(xué)表達(dá)式源碼

計(jì)算數(shù)學(xué)表達(dá)式原理 采用c#實(shí)現(xiàn) 很實(shí)用
//a.建立兩個(gè)棧:第一個(gè)位操作數(shù)棧,第二個(gè)操作符符棧!(將棧定義為string類(lèi)型)
//b.對(duì)數(shù)字來(lái)說(shuō)是無(wú)條件壓入數(shù)字棧中.
//c.而對(duì)符號(hào)來(lái)說(shuō),只有當(dāng)前棧頂元素的優(yōu)先值小于掃到的符號(hào)時(shí)(比如”+”小于”*”),此符號(hào)才壓入棧;否則大于等于的情況是將當(dāng)前棧頂元素彈出棧,與當(dāng)前數(shù)字棧的前兩個(gè)數(shù)字組成式子進(jìn)行計(jì)算.計(jì)算結(jié)果當(dāng)作數(shù)字壓入數(shù)字棧作為棧頂元素(要舍棄已經(jīng)彈出的兩個(gè)數(shù)字),而那個(gè)掃描到的符號(hào)則將代替那個(gè)彈出的符號(hào)作為棧頂元素)。
//d.最后說(shuō)一下括號(hào),原則是掃描到左括號(hào)時(shí)無(wú)條件壓入符號(hào)棧,而掃到右括號(hào)時(shí),則彈出離棧頂最近的一個(gè)左括號(hào)以上的全部符號(hào)與數(shù)字棧的數(shù)字做運(yùn)算

3.asp.net教程 datagridtree表格樹(shù)控件 

繼承asp.net的datagrid控件實(shí)現(xiàn)的表格樹(shù)控件
/*表格樹(shù)控件說(shuō)明
* 此控件繼承datagrid 新增屬性說(shuō)明:
* 1.treeparentcode:頂級(jí)根節(jié)點(diǎn)parentcode
* 2.treedisplaydeep:展現(xiàn)表格樹(shù)深度默認(rèn)為1
* 3.sumcolumns:自動(dòng)匯總到根節(jié)點(diǎn)的字段集合 針對(duì) decimal類(lèi)型
* 4.新增樹(shù)狀列模板templatetreecolumn 此模板繼承了templatecolumn 重寫(xiě)了方法initializecell
* 客戶(hù)端新增特性配置說(shuō)明
* 1.固定列 配置 itemstyle-css教程class='tdlockedclass'
* 2.固定表頭 配置 headerstyle-cssclass='trlockedclass'
* 3.文本框 input 或 asp:textbox 配置事件onchange='sumparent(this);' 數(shù)字改變相應(yīng)所有父節(jié)點(diǎn)也隨著改變 針對(duì)數(shù)字型 其他不支持
* 不過(guò)可以自定義js
* 報(bào)表說(shuō)明:
* 1.datagridtree.enableviewstate=false;提高加載速度
* 2.動(dòng)態(tài)定義列 實(shí)現(xiàn) boundcolumn column = new boundcolumn();
column.headertext = "動(dòng)態(tài)列";
column.datafield = "unitname";
datagridnew.columns.add(column);
* 也可以自定義默認(rèn)模板 動(dòng)態(tài)加載模板 定義模板例子templatetreecolumn,不用繼承templatecolumn,實(shí)現(xiàn)接口 itemplate initializecell 方法就可以了
* 不足之處:1.對(duì)于復(fù)雜多行表頭 不知 如何實(shí)現(xiàn)
* 2.表頭和列固定 數(shù)據(jù)量大時(shí) 會(huì)影響反映速度 一千左右的數(shù)據(jù)量 還時(shí)沒(méi)問(wèn)題的 數(shù)據(jù)量在大的話(huà) 課考慮采用ajax動(dòng)態(tài)加載 目前此功能還沒(méi)實(shí)現(xiàn)
實(shí)例代碼

復(fù)制代碼 代碼如下:
private void maketree(datatable dtnodesets, string strparentcolumn, string strrootvalue, string strindexcolumn, string strtextcolumn, dropdownlist drpbind, int i)
{
//每向下一層,多一個(gè)縮入單位  
i++;
dataview dvnodesets = new dataview(dtnodesets);
dvnodesets.rowfilter = strparentcolumn + "=" + strrootvalue;
string strpading = ""; //縮入字符 
//通過(guò)i來(lái)控制縮入字符的長(zhǎng)度,我這里設(shè)定的是一個(gè)全角的空格  
for (int j = 0; j i; j++)
strpading += " ";//如果要增加縮入的長(zhǎng)度,改成兩個(gè)全角的空格就可以了 
foreach (datarowview drv in dvnodesets)
{
treenode tnnode = new treenode();
listitem li = new listitem(strpading + "├" + drv[strtextcolumn].tostring(), drv[strindexcolumn].tostring());
drpbind.items.add(li);
maketree(dtnodesets, strparentcolumn, drv[strindexcolumn].tostring(), strindexcolumn, strtextcolumn, drpbind, i);
}
//遞歸結(jié)束,要回到上一層,所以縮入量減少一個(gè)單位  
i--;
}
/// summary>  
/// sql語(yǔ)句查詢(xún),再綁定到droplist里面  
/// /summary>  
private void createtree()
{
//查詢(xún)zonelist  
string sql = "select * from master_department where parent_department='003'";
dataset ds = db.getds();
datatable dt = ds.tables[0];
maketree(dt, "parent_department", "003", "department_code", "department_name", dropdownlist1, -1);
}

網(wǎng)上找的另一個(gè)比較好的實(shí)例
復(fù)制代碼 代碼如下:
using system;
using system.collections.generic;
using system.text;
using system.web.ui.webcontrols;
namespace interface.common
{
    public interface idropdowntree : idisposable
    {
        /**//// summary>
        /// 返回dictionary里分別對(duì)應(yīng)id,文本,如果沒(méi)有子節(jié)點(diǎn)返回null
        /// /summary>
        /// param name="parentid">父節(jié)點(diǎn)id/param>
        /// returns>/returns>
        dictionarystring, string> getchildcategory(string parentid);
        /**//// summary>
        /// 代碼里寫(xiě)return new interface.common.dropdowntree(this);
        /// /summary>
        dropdowntree dropdowntree
        {
            get;
        }
    }
    public sealed class dropdowntree
    {
        idropdowntree _dropdowntree;
        public dropdowntree(idropdowntree dropdowntree)
        {
            _dropdowntree = dropdowntree;
        }
        /**//// summary>
        /// 用于樹(shù)的前綴
        /// /summary>
        /// param name="islast">是否是同級(jí)節(jié)點(diǎn)中的最后一個(gè)/param>
        /// param name="haschild">本節(jié)點(diǎn)是否擁有子節(jié)點(diǎn)/param>
        /// param name="parentstring">父節(jié)點(diǎn)前綴符號(hào)/param>
        /// returns>本節(jié)點(diǎn)的前綴/returns>
        private string getprefix(bool islast, bool haschild, string parentstring)
        {
            string result = string.empty;
            if (!string.isnullorempty(parentstring))
            {
                parentstring = parentstring.remove(parentstring.length - 1).replace("├", "│").replace("└", " ");
                result += parentstring;
            }
            if (islast)
            {
                result += "└";
            }
            else
            {
                result += "├";
            }
            if (haschild)
            {
                result += "┬";
            }
            else
            {
                result += "─";
            }
            return result;
        }
        綁定下拉菜單#region 綁定下拉菜單
        /**//// summary>
        /// 綁定連動(dòng)級(jí)的下拉菜單
        /// /summary>
        /// param name="ddlgoodstype">傳進(jìn)一個(gè)被綁定的dropdownlist/param>
        /// param name="removeid">被排除綁定的節(jié)點(diǎn)id/param>
        /// param name="autodispose">是否自動(dòng)釋放/param>
        public void bindtodropdownlist(dropdownlist ddlgoodstype, string removeid,string parentid, bool autodispose)
        {
            if (ddlgoodstype != null)
            {
                listitem listitem = null;
                string currentid = parentid;//根節(jié)點(diǎn)/父id
                string currentsign = string.empty;//當(dāng)前節(jié)點(diǎn)符號(hào);
                string parrentsign = string.empty; //父節(jié)點(diǎn)符號(hào);
                bool haschild = true;//是否有子
                queuestring> parentkeylist = new queuestring>();//存 有子節(jié)點(diǎn)的 節(jié)點(diǎn)id
                queuestring> parentsignlist = new queuestring>();//對(duì)應(yīng)節(jié)點(diǎn)id的前綴符號(hào)
                int itemindexof = 0;//父節(jié)點(diǎn)所在的位置 
                while (haschild)
                {
                    int lastonecount = 1;//用于計(jì)算在同級(jí)別中是否最后一個(gè)
                    dictionarystring, string> childlist = _dropdowntree.getchildcategory(currentid);// 得到子節(jié)點(diǎn)列表
                    if (childlist != null childlist.count > 0)
                    {
                        if (!string.isnullorempty(removeid) childlist.containskey(removeid))
                        {
                            childlist.remove(removeid);
                        }
                        foreach (keyvaluepairstring, string> entry in childlist)
                        {
                            if (_dropdowntree.getchildcategory(entry.key) != null)//存在子
                            {
                                currentsign = getprefix(lastonecount == childlist.count, true, parrentsign);
                                listitem = new listitem(currentsign + entry.value, entry.key);
                                parentkeylist.enqueue(entry.key);//當(dāng)前的節(jié)點(diǎn)id
                                parentsignlist.enqueue(currentsign);//當(dāng)前的節(jié)點(diǎn)符號(hào)
                            }
                            else//不存在子
                            {
                                currentsign = getprefix(lastonecount == childlist.count, false, parrentsign);
                                listitem = new listitem(currentsign + entry.value, entry.key);
                            }
                            if (ddlgoodstype.items.count != 0)
                            {
                                itemindexof = string.isnullorempty(currentid) ? itemindexof + 1 : ddlgoodstype.items.indexof(ddlgoodstype.items.findbyvalue(currentid)) + lastonecount;
                            }
                            ddlgoodstype.items.insert(itemindexof, listitem);//添加子節(jié)點(diǎn)
                            lastonecount++;
                        }
                        if (parentkeylist.count > 0)//存在子節(jié)點(diǎn)時(shí)
                        {
                            currentid = parentkeylist.dequeue();
                            parrentsign = parentsignlist.dequeue();
                        }
                        else
                        {
                            haschild = false;
                        }
                    }
                    else
                    {
                        break;
                    }
                }
                if (autodispose)
                {
                    _dropdowntree.dispose();
                }
            }
        }
        /**//// summary>
        /// 綁定連動(dòng)級(jí)的下拉菜單
        /// /summary>
        /// param name="ddlgoodstype">傳進(jìn)一個(gè)被綁定的dropdownlist/param>
        public void bindtodropdownlist(dropdownlist ddlgoodstype)
        {
            bindtodropdownlist(ddlgoodstype, string.empty,null, true);
        }
        /**//// summary>
        /// 綁定連動(dòng)級(jí)的下拉菜單
        /// /summary>
        /// param name="ddlgoodstype">傳進(jìn)一個(gè)被綁定的dropdownlist/param>
        /// param name="removeid">被排除的id/param>
        public void bindtodropdownlist(dropdownlist ddlgoodstype, string removeid)
        {
            bindtodropdownlist(ddlgoodstype, removeid,null, true);
        }
        /**//// summary>
        /// 綁定連動(dòng)級(jí)的下拉菜單
        /// /summary>
        /// param name="ddlgoodstype">傳進(jìn)一個(gè)被綁定的dropdownlist/param>
        /// param name="removeid">被排除的id,若沒(méi)有,傳null/param>
        /// param name="parentid">起始父id/param>
        public void bindtodropdownlist(dropdownlist ddlgoodstype, string removeid,string parentid)
        {
            bindtodropdownlist(ddlgoodstype, removeid,parentid, true);
        }
        #endregion
    }
}

調(diào)用方法很簡(jiǎn)單:
1.繼承自idropdowntree接口
2.實(shí)現(xiàn)3個(gè)接口方法實(shí)現(xiàn)接口代碼示例[dispose方法自己實(shí)現(xiàn)],最主要的是自己實(shí)現(xiàn)獲得子級(jí)的方法
復(fù)制代碼 代碼如下:
idropdowntree 成員
#region idropdowntree 成員
public dictionarystring, string> getchildcategory(string parentid)
{
    string where = "parentid='" + parentid + "'";
    if (string.isnullorempty(parentid))
    {
 where = "parentid is null or parentid='" + guid.empty + "'";
    }
    listgoodscategorybean> _goodscategorylist = selectlist(0, where, string.empty, false);
    if (_goodscategorylist != null _goodscategorylist.count > 0)
    {
 dictionarystring, string> categorylist = new dictionarystring, string>();
 for (int i = 0; i _goodscategorylist.count; i++)
 {
     categorylist.add(_goodscategorylist[i].id.tostring(), _goodscategorylist[i].gategoryname);
 }
 return categorylist;
    }//51aspx.com
    return null;
}
public interface.common.dropdowntree dropdowntree
{
    get { return new interface.common.dropdowntree(this); }
}
#endregion

頁(yè)面調(diào)用代碼: 類(lèi)名.dropdowntree.bindtodropdownlist(下拉控件id);

希望本文所述對(duì)大家的asp.net程序設(shè)計(jì)有所幫助。

您可能感興趣的文章:
  • 適用與firefox ASP.NET無(wú)刷新二級(jí)聯(lián)動(dòng)下拉列表
  • ASP.NET 2.0寫(xiě)無(wú)限級(jí)下拉菜單
  • asp.net DropDownList 三級(jí)聯(lián)動(dòng)下拉菜單實(shí)現(xiàn)代碼
  • asp.net 下拉列表無(wú)級(jí)數(shù)據(jù)綁定實(shí)現(xiàn)代碼
  • asp.net 實(shí)現(xiàn)下拉框只讀功能
  • ASP.NET C#生成下拉列表樹(shù)實(shí)現(xiàn)代碼
  • asp.net中js+jquery添加下拉框值和后臺(tái)獲取示例
  • asp.net mvc下拉框Html.DropDownList 和DropDownListFor的常用方法
  • Asp.net下拉樹(shù)的實(shí)現(xiàn)過(guò)程
  • ASP.NET多彩下拉框開(kāi)發(fā)實(shí)例
  • ASP.NET實(shí)現(xiàn)級(jí)聯(lián)下拉框效果實(shí)例講解

標(biāo)簽:益陽(yáng) 咸寧 攀枝花 棗莊 拉薩 南平 廈門(mén) POS機(jī)

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《asp.net使用DataGridTree實(shí)現(xiàn)下拉樹(shù)的方法》,本文關(guān)鍵詞  ;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問(wèn)題,煩請(qǐng)?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無(wú)關(guān)。
  • 相關(guān)文章
  • 收縮
    • 微信客服
    • 微信二維碼
    • 電話(huà)咨詢(xún)

    • 400-1100-266
    东乌珠穆沁旗| 鄂尔多斯市| 东乡族自治县| 赫章县| 凯里市| 高要市| 美姑县| 萍乡市| 同仁县| 邵东县| 壶关县| 边坝县| 保德县| 玉树县| 远安县| 班戈县| 房产| 沧州市| 仙桃市| 阿坝| 辽中县| 霍山县| 多伦县| 时尚| 军事| 玉龙| 阆中市| 通山县| 顺昌县| 宣武区| 定襄县| 通海县| 望城县| 土默特右旗| 内黄县| 辽宁省| 汉寿县| 张家川| 定兴县| 平陆县| 新民市|