Asp.net recursively displays the code of the Treeview Web Control

Source: Internet
Author: User

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

If a Category record has a previous category, parent_id = 'parent category id'. If it is the highest level, no parent category parent_id = NULL

The checkpermitfromtable function is used to determine whether the current login user has permissions on the log column, because the system requires the permission and does not have the permission to display different URLs.

The inittreeview function is the entry to the entire code segment. The external call to the class starts from this function. I will not talk about the parameters much, but I can see it myself!

The inittree function is a recursive function that finds whether the current class has a lower-level category. If you are not very clear about it, it is best not to look at this function! :)
Will be in the dark!

Effects of code execution:

# Region initialize the Treeview Control for displaying the Column Structure

/// <Summary>
/// Check whether the specified user has the permission for this topic. [recursive call]
/// </Summary>
/// <Param name = "puserid"> User ID </param>
/// <Param name = "pcatalogid"> topic id </param>
/// <Returns> if you have the permission to return true, false is returned if you do not have the permission. </returns>
Public bool checkpermitfromtable (string puserid, string pcatalogid)
{
Try
{

Datarow [] rows = dstree. Tables ["power"]. Select ("catalog_id =" + pcatalogid );

If (rows! = NULL)
{
If (rows. length> 0)
{
Return true;
}
Else
{
/*
* If the current topic does not have the permission, check whether the upper-level topic in the query system has the permission,
If true is returned, if you still find that the topic has no operation permission,
Then confirm that the user has no operation permission on the current topic. --- Wang Haibo
*/
Rows = dstree. Tables ["catalog"]. Select ("ID =" + pcatalogid );

If (rows. length> 0 & rows [0] ["parent_id"]. tostring (). Trim ()! = "")
{
If (checkpermitfromtable (puserid, rows [0] ["parent_id"]. tostring () // [recursive call]
Return true;
}

Return false;
}
}
Else
{
Return false;
}
}
Catch (sqlexception SE)
{
Errormessage = Se. message;
Return false;
}
}

/// <Summary>
/// Obtain the topic information in the work bar based on parameters
/// </Summary>
/// <Param name = "pparentid"> obtain all subtopics under the current topic ID. If pparentid is set to 0, obtain the first-level topic information. </param>
/// <Param name = "puserid"> User ID </param>
/// <Returns> return the dataset result set </returns>
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 ();
 
// Obtain column data
Sqldataadapter sqladpt = new sqldataadapter (strsql, CN );

Sqladpt. Fill (DS, "catalog ");

// Obtain the permission data for the user Column
Sqladpt = new sqldataadapter (strsql1, CN );
Sqladpt. Fill (DS, "power ");
 
Return Ds;

}
Catch (sqlexception SE)
{
Errormessage = Se. message;
Return NULL;
}
}

/// <Summary>
/// Initialize the Treeview
/// </Summary>
/// <Param name = "TV"> Treeview Control </param>
/// <Param name = "rootimageurl"> picture of the root node of the Treeview Control </param>
/// <Param name = "puserid"> current user id </param>
Public void inittreeview (Treeview TV, string rootimageurl, string puserid)
{
// Initialize the first-level tree node start
Dataset DS = getcataloginfo (0, puserid );

TV. nodes. Clear ();
TV. imageurl = rootimageurl;

For (INT I = 0; I <Ds. Tables [0]. Rows. Count; I ++)
{
Treenode TN1 = new treenode ();

Tn1.imageurl = "/images/ico01.gif ";
Tn1.id = "L1-" + I. tostring ();
Tn1.text = Ds. tables [0]. rows [I] ["ID"]. tostring () + "-" + Ds. tables [0]. rows [I] ["catalog_name"]. tostring ();

Dstree = getcataloginfo (Int. parse (Ds. Tables [0]. Rows [I] ["ID"]. tostring (), puserid );

If (checkpermitfromtable (puserid, DS. Tables [0]. Rows [I] ["ID"]. tostring ()))
Tn1.navigateurl = "/admin/hg_info_manage.aspx? Cid = "+ Ds. tables [0]. rows [I] ["ID"]. tostring () + "& catalogname =" + Ds. tables [0]. rows [I] ["catalog_name"]. tostring ();
Else
Tn1.navigateurl = "/INFO/t_info_index.aspx? Cid = "+ Ds. tables [0]. rows [I] ["ID"]. tostring () + "& catalogname =" + Ds. tables [0]. rows [I] ["catalog_name"]. tostring ();

Tn1.expanded = true;

TV. nodes. Add (TN1 );

Inittree (TV. nodes [I]. nodes, DS. Tables [0]. Rows [I] ["ID"]. tostring (), puserid );

Plevel = 0; // layer restoration of the tree

}
// Initialize the first-level tree node end

// Initialize the dataset that fills the Treeview

}

/// <Summary>
/// Recursively call this function to display the tree
/// </Summary>
/// <Param name = "NDS"> tree node </param>
/// <Param name = "parentid"> ID of the upper-level column </param>
/// <Param name = "puserid"> current user id </param>
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 ++; // layer of the tree plus 1

Int pcurlevel = plevel;

For (INT I = 0; I <rows. length; I ++)
{
Datarow DRV = rows [I];

Tmpnd = new treenode ();

Tmpnd. ID = DRV ["ID"]. tostring ();
Tmpnd. Text = DRV ["ID"]. tostring () + "-" + DRV ["catalog_name"]. tostring ();

// Change the number layer icon
Switch (plevel)
{
Case 1:
Tmpnd. imageurl = "/images/fling.gif ";
Break;

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;

}

}

# Endregion

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.