ASP.NET displays the code of the TreeView web control with recursive way

xiaoxiao2021-03-06  17

Code Description: Table t_hg_catalog script: Create Table [T_HG_CATALOG] ([ID] [INT] Identity (1, 1) Not Null, [Catalog_name] [varchar] (30) collate chinese_prc_ci_as not null, [PARENT_ID] [INT] NULL , [Remark] [varchar] (128) COLLATE Chinese_PRC_CI_AS NULL, [State] [smallint] NOT NULL CONSTRAINT [DF_T_hg_catalog_State] DEFAULT (0), CONSTRAINT [PK_t_hg_catalog] PRIMARY KEY CLUSTERED ([ID]) ON [PRIMARY]) ON [ PRIMARY] GO, a category record If there is a last level category, the parent_id = 'superior category ID', if it is the highest level, there is no higher class category parent_id = nullCheckpermitFromTable function is to determine if the current login user is right to have permission Because the system requires permission and no permission to display different url.inittreeView functions is the entry of the entire code, the external call is called, from this function. Parameter, I don't say much, I can see it! The inittree function is a Recursive function lookup current class has no lower class category, if the mind is not very clear, it is best not to see this function! :) Will go into the magic land! The effect after the code is executed: #Region initializes the TreeView control of the display column structure

///

/// Check the specified user for this column, [recursive call] /// /// User ID // / Column ID /// Returns Returns True, no permission to return false public bool checkpermitfromtable (string puserid, string pcatalog) {Try {dataroow [] Rows = DSTREE.TABLES ["Power"]. SELECT ("Catalog_Id =" PCATALOGID);

IF (rows! = null) {if (Rows.Length> 0) {return true;} else {/ * * If the current column has no permissions, the upper column in the query system has permissions, if there is Returning True, if you have been found The highest level column has no operational permissions, then confirm that the user does not have operation permission for the current column. --- Wang Haibo * / rows = dstree.tables ["Catalog"]. SELECT ("ID =" pcatalogid);

IF (Rows.Length> 0 && rows [0] ["Parent_ID"]. TOSTRING (). Trim ()! = "" {ix (CheckpermitFromTable (PUSERID, ROWS [0] ["Parent_ID"]. Tostring () ) // [recursive call] return true;

Return False;}}}} catch (sqlexception se) {ErrorMessage = se.MESSAGE; RETURN FALSE;}} ///

/ / / / / / / / < / summary> /// Get all sub-columns under the current column ID, if pParentID = 0, get a level of column information /// User ID /// //// Return DataSet Results Set Public Dataset getCatalogInfo (int pParentID, String PUSERID) {Try {string strsql, strsql1; if (pparentId == 0) {strsql = "SELECT * From t_hg_catalog where state = 0 and parent_id is null order by id ASC ";} else {strsql =" select * from t_hg_catalog where state = 0 Order by id ASC ";

strsql1 = "select * from t_hg_power where user_id = '" PUSERID "'"

DataSet DS = New DataSet (); // Get Column Data SqlDataAdapter SQLADPT = New SqlDataAdapter (Strsql, CN);

SQLADPT.FILL (DS, "Catalog");

// Get user-column permission rights data sqladpt = new sqldataadapter (strsql1, cn); SQLADPT.FILL (DS, "Power"); return DS;} catch (sqlexception se) {errorMessage = se.MESSAGE; RETURN NULL;}}

///

/// Initialization TreeView /// /// TreeView control /// Current Operation User ID Public Void InitTreeView (TreeView TV, String RootimageURL, String PUSERID) {// Initialization Level 1 Tree Node Start Dataset DS = GetCatalogInfo (0, PUSERID);

TV.nodes.clear (); tv.imageurl = rootimageurl;

For (int i = 0; i

TN1.Expanded = true; TV.Nodes.Add (TN1);

INITTREE (TV.Nodes [i] .nodes, ds.tables [0] .rows [i] ["id"]. TOSTRING (), PUSERID);

Plevel = 0; // Tree Layer Restore} // Initialize the first level tree node end // Initialization populates TreeView's dataset}

///

/// recursive call the function to show /// /// node /// super-column ID /// Current Operation User ID Private Void InitTree (TREENODECOLLECTION NDS, STRING PARENTID, STRING PUSERID) {dataview DV = New DataView ); TREENODE TMPND; STRING INTID; DV.TABLE = DSTREE.TABLES [0]; DATAROW [] ROWS = DV.TABLE.SELECT ("Parent_ID =" ParentID);

PLEVEL ; // Tree level plus 1INT PCURLEVEL = plevel; for (int i = 0; i

Case 2: tmpnd.imageurl = "/ images / iCO03.GIF"; Break;

Case 3: tmpnd.imageurl = "/ images / state_normal.gif"; Break;

} IF (CheckpermitFromTable (PUSERID, DRV ["Id"]. TOSTRING ()) TMPND.NAVIGATEURL = "/ admin / hg_info_manage.aspx? CID =" DRV ["ID"]. Tostring () "& catalogname =" DRV ["Catalog_name"]. TOSTRING (); else tmpnd.navigateURL = "/ info / hg_info_index.aspx? CID =" DRV ["ID"]. Tostring () "& catalogname =" DRV ["Catalog_name" ] .Tostring (); nds.add (tmpnd); intid = DRV ["Parent_ID"]. TOSTRING ();

INITTREE (Tmpnd.nodes, Tmpnd.id, PUSERID);

plevel = pcurlevel;}

}

#ndregion

转载请注明原文地址:https://www.9cbs.com/read-45145.html

New Post(0)