asp.net uses DataGridTree to implement the drop down tree method

  • 2021-01-19 22:13:43
  • OfStack

This article illustrates how asp. net uses DataGridTree to implement a drop-down tree. Share with you for your reference. The specific implementation method is as follows:

Dropdown tree implementation principle: output json to the client, the client to achieve dynamic loading, the middle will not interact with the server. Data volume support has been tested on several thousand is still very fast. This dropdown tree control is implemented with c#+js tree.

2.c# Calculator to calculate the string mathematical expression source

The principle of calculating mathematical expressions is very practical to implement by c#
a. Build two stacks: the first operand stack, the second operand stack! (Stack defined as string type)
//b. For numbers it is unconditionally pushed into the number stack.
c. Symbols are pushed on the stack only if the priority of the current top element is less than that of the swept symbol (for example, "+" is less than "*"). Otherwise greater than or equal to the situation is to pop the current stack top element stack, and the current stack of the first two numbers to form the formula for calculation. The result of the calculation is pushed into the stack as a number as the top element (discarding two numbers that have popped up), and the scanned symbol replaces the pop-up symbol as the top element).
// ES23en. When the open bracket is scanned, it is unconditional to push the symbol stack, and when the right bracket is scanned, all the symbols above the nearest left bracket are popped and computed with the number of the stack

3.asp.net Tutorial datagridtree Table Tree Control

A table tree control that inherits the datagrid control of asp.net
/* Table tree control specification
* This control inherits datagrid new property description:
* 1.treeparentcode: top-level root node parentcode
treedisplaydeep: Default table tree depth is 1
* 3.sumcolumns: The collection of fields automatically summarized to the root node is for decimal type
* 4. New tree template templatetreecolumn This template inherits from templatecolumn and overwrites the method initializecell
* Client new feature configuration notes
itemstyle-css - class='tdlockedclass'
* 2. Fixed header headerstyle-cssclass='trlockedclass'
* 3. Text box input or < asp:textbox configuration event onchange='sumparent(this); 'Numbers change accordingly all parent nodes also follow the change for Numbers type other not supported
* However, you can customize js
* Statement Description:
* 1.datagridtree.enableviewstate=false; Increase loading speed
boundcolumn column = new boundcolumn();
column. headertext = "Dynamic Columns ";
column.datafield = "unitname";
datagridnew.columns.add(column);
* You can also customize the default template dynamic loading template definition template example templatetreecolumn, do not inherit templatecolumn, the implementation of the interface itemplate initializecell method can be
* Shortcomings: 1. Do not know how to implement complex multi-row header
* 2. When the table header and column are fixed, the data volume will affect the reflecting speed of about 1000. When the data volume is no problem, the dynamic loading of ajax will be considered if the data volume is large
The sample code

private void maketree(datatable dtnodesets, string strparentcolumn, string strrootvalue, string strindexcolumn, string strtextcolumn, dropdownlist drpbind, int i)
{
// Each downward 1 Layer, 1 Is indented in units   
i++;
dataview dvnodesets = new dataview(dtnodesets);
dvnodesets.rowfilter = strparentcolumn + "=" + strrootvalue;
string strpading = ""; // Retreat into character  
// through i To control the length of the indented characters, which I've set here 1 All of the blank Spaces   
for (int j = 0; j < i; j++)
strpading += " ";// If you want to increase the indent length, change it to two full - Angle Spaces  
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);
}
// The recursion is over, so we go back to the top 1 Layer, so the indentation is reduced 1 A unit of   
i--;
}
/// <summary>  
/// sql Statement query and then bind to droplist inside   
/// </summary>  
private void createtree()
{
// The query 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);
}

Another good example found on the Internet
using system;
using system.collections.generic;
using system.text;
using system.web.ui.webcontrols;
namespace interface.common
{
    public interface idropdowntree : idisposable
    {
        /**//// <summary>
        /// return dictionary In respectively corresponding to id, The text , If no child nodes are returned null
        /// </summary>
        /// <param name="parentid"> The parent node id</param>
        /// <returns></returns>
        dictionary<string, string> getchildcategory(string parentid);
        /**//// <summary>
        /// Write the code return new interface.common.dropdowntree(this);
        /// </summary>
        dropdowntree dropdowntree
        {
            get;
        }
    }
    public sealed class dropdowntree
    {
        idropdowntree _dropdowntree;
        public dropdowntree(idropdowntree dropdowntree)
        {
            _dropdowntree = dropdowntree;
        }
        /**//// <summary>
        /// A prefix for a tree
        /// </summary>
        /// <param name="islast"> Is the last node of the same level 1 a </param>
        /// <param name="haschild"> Whether this node has child nodes </param>
        /// <param name="parentstring"> Parent node prefix symbol </param>
        /// <returns> The prefix of this node </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;
        }
        Bind the drop-down menu #region Bind the drop-down menu
        /**//// <summary>
        /// Bind the drop-down menu for the linkage level
        /// </summary>
        /// <param name="ddlgoodstype"> Pass into 1 Student: It's bound dropdownlist</param>
        /// <param name="removeid"> The node that is excluded from binding id</param>
        /// <param name="autodispose"> Whether to release automatically </param>
        public void bindtodropdownlist(dropdownlist ddlgoodstype, string removeid,string parentid, bool autodispose)
        {
            if (ddlgoodstype != null)
            {
                listitem listitem = null;
                string currentid = parentid;// The root node / The father id
                string currentsign = string.empty;// Current Node Symbol ;
                string parrentsign = string.empty; // Parent node symbol ;
                bool haschild = true;// If there is a child
                queue<string> parentkeylist = new queue<string>();// save It has child nodes node id
                queue<string> parentsignlist = new queue<string>();// Corresponding node id The prefix symbol of
                int itemindexof = 0;// The location of the parent node
                while (haschild)
                {
                    int lastonecount = 1;// Used to calculate whether it is last in the same class 1 a
                    dictionary<string, string> childlist = _dropdowntree.getchildcategory(currentid);// Get a list of child nodes
                    if (childlist != null && childlist.count > 0)
                    {
                        if (!string.isnullorempty(removeid) && childlist.containskey(removeid))
                        {
                            childlist.remove(removeid);
                        }
                        foreach (keyvaluepair<string, string> entry in childlist)
                        {
                            if (_dropdowntree.getchildcategory(entry.key) != null)// There is the child
                            {
                                currentsign = getprefix(lastonecount == childlist.count, true, parrentsign);
                                listitem = new listitem(currentsign + entry.value, entry.key);
                                parentkeylist.enqueue(entry.key);// Current Node id
                                parentsignlist.enqueue(currentsign);// The current node symbol
                            }
                            else// There is no child
                            {
                                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);// Add child node
                            lastonecount++;
                        }
                        if (parentkeylist.count > 0)// When there are children
                        {
                            currentid = parentkeylist.dequeue();
                            parrentsign = parentsignlist.dequeue();
                        }
                        else
                        {
                            haschild = false;
                        }
                    }
                    else
                    {
                        break;
                    }
                }
                if (autodispose)
                {
                    _dropdowntree.dispose();
                }
            }
        }
        /**//// <summary>
        /// Bind the drop-down menu for the linkage level
        /// </summary>
        /// <param name="ddlgoodstype"> Pass into 1 Student: It's bound dropdownlist</param>
        public void bindtodropdownlist(dropdownlist ddlgoodstype)
        {
            bindtodropdownlist(ddlgoodstype, string.empty,null, true);
        }
        /**//// <summary>
        /// Bind the drop-down menu for the linkage level
        /// </summary>
        /// <param name="ddlgoodstype"> Pass into 1 Student: It's bound dropdownlist</param>
        /// <param name="removeid"> Be ruled out id</param>
        public void bindtodropdownlist(dropdownlist ddlgoodstype, string removeid)
        {
            bindtodropdownlist(ddlgoodstype, removeid,null, true);
        }
        /**//// <summary>
        /// Bind the drop-down menu for the linkage level
        /// </summary>
        /// <param name="ddlgoodstype"> Pass into 1 Student: It's bound dropdownlist</param>
        /// <param name="removeid"> Be ruled out id, If there is no , the null</param>
        /// <param name="parentid"> Starting the father id</param>
        public void bindtodropdownlist(dropdownlist ddlgoodstype, string removeid,string parentid)
        {
            bindtodropdownlist(ddlgoodstype, removeid,parentid, true);
        }
        #endregion
    }
}

Calling the method is simple:
1. Inherited from the idropdowntree interface
2. Implement 3 interface method implementation interface code example [dispose method to achieve their own], the most important is to achieve their own methods to obtain children
idropdowntree  Members of the 
#region idropdowntree Members of the
public dictionary<string, string> getchildcategory(string parentid)
{
    string where = "parentid='" + parentid + "'";
    if (string.isnullorempty(parentid))
    {
 where = "parentid is null or parentid='" + guid.empty + "'";
    }
    list<goodscategorybean> _goodscategorylist = selectlist(0, where, string.empty, false);
    if (_goodscategorylist != null && _goodscategorylist.count > 0)
    {
 dictionary<string, string> categorylist = new dictionary<string, 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

dropdowntree.bindtodropdownlist(drop-down control id);

asp.net I hope this article is helpful for you to design asp.


Related articles: