Using the IE WEB control TreeView in C # code

Source: Internet
Author: User
Treeview|web Site Directory needs to have Microsoft.Web.UI.WebControls.dll and corresponding files
If you want to reprint, please retain my copyright.

/*
*description: Complete operation of XML file
*auther:mingziweb_ the sky is blue
*email:chongchong2008@msn.com
*dates:22004-09-10
*copyright:chongchong2008 Yichang Hubei
*/



. aspx
<%@ Register tagprefix= "ie" namespace= "Microsoft.Web.UI.WebControls" assembly= "Microsoft.Web.UI.WebControls"% >
<%@ Page language= "C #" codebehind= "TreeView.aspx.cs" autoeventwireup= "false" inherits= "Test.webform1"%>
<! DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 transitional//en" >
<HTML>
<HEAD>
<title>WebForm1</title>
<meta name= "generator" content= "Microsoft Visual Studio. NET 7.1" >
<meta name= "Code_language" content= "C #" >
<meta name= "vs_defaultClientScript" content= "JavaScript" >
<meta name= "vs_targetschema" content= "http://schemas.microsoft.com/intellisense/ie5" >
</HEAD>
<body ms_positioning= "GridLayout" >
<form id= "Form1" method= "POST" runat= "Server" >
<font face= "Song Body" >
<ie:treeview id= "MyTreeView" style= "Z-INDEX:101; left:24px; Position:absolute; Top:32px "
runat= "Server" ></ie:TreeView></FONT>
</form>
</body>
</HTML>



. CS
Using System;
Using System.Collections;
Using System.ComponentModel;
Using System.Data;
Using System.Drawing;
Using System.Web;
Using System.Web.SessionState;
Using System.Web.UI;
Using System.Web.UI.WebControls;
Using System.Web.UI.HtmlControls;

Using System.Data.SqlClient;
Using System.Configuration;

Using Microsoft.Web.UI.WebControls;

Namespace Test
{
<summary>
Summary description of the WebForm1.
</summary>
public class WebForm1:System.Web.UI.Page
{
protected Microsoft.Web.UI.WebControls.TreeView MyTreeView;

Private SqlConnection myconnection = new SqlConnection (configurationsettings.appsettings["strconn"));



Code generated #region the Web forms Designer
Override protected void OnInit (EventArgs e)
{
//
CodeGen: This call is required for the ASP.net Web forms Designer.
//
InitializeComponent ();
Base. OnInit (e);
}

<summary>
Designer supports required methods-do not use the Code editor to modify
The contents of this method.
</summary>
private void InitializeComponent ()
{
This. Load + = new System.EventHandler (this. Page_Load);

}
#endregion





private void Page_Load (object sender, System.EventArgs e)
{
Place user code here to initialize page

TreeNode mytreenoderoot = Addtreenoderoot ();
AppendChild (Mytreenoderoot);

}





Private TreeNode Addtreenoderoot ()
{

TreeNode Mytreenode = new TreeNode ();

Mytreenode.id = "420000";
Mytreenode.expanded = true;
Mytreenode.target = "";
Mytreenode.text = "my Community";

MYTREEVIEW.NODES.ADD (Mytreenode);
return mytreenode;
}



Private TreeNode Addsubnode (TreeNode subtreenode)
{

TreeNode Mytreenode = new TreeNode ();

Mytreenode.id = "00001";
Mytreenode.expanded = true;
Mytreenode.target = "";
Mytreenode.text = "province";
Mytreenode.navigateurl = "";

SUBTREENODE.NODES.ADD (Mytreenode);
return mytreenode;
}





private void AppendChild (TreeNode mytreenode)
{

TreeNode Mysubnode = Addsubnode (Mytreenode);

if (Getclasstable (). ROWS.COUNT&GT;0)
{
DataTable mydatatable = new DataTable ();
mydatatable = Getclasstable ();

for (int i=0;i<mydatatable.rows.count;i++)
{
TreeNode mytreenodesub = new TreeNode ();

Mytreenodesub.checkbox=true;
if (i==20)
{
Mytreenodesub.checked=true;

}

Mytreenodesub.id = mydatatable.rows[i]["Class_code"]. ToString ();
mytreenodesub.expanded = false;
Mytreenodesub.target = "Body";
Mytreenodesub.text = mydatatable.rows[i]["Class_name"]. ToString ();
Mytreenodesub.navigateurl = "Doclist.aspx?" Doccatalogguid= "+ mydatatable.rows[i][" Class_code "]. ToString ();

MYSUBNODE.NODES.ADD (mytreenodesub);

Appendchildren (mytreenodesub);

}
}
}




private void Appendchildren (TreeNode mytreenode)
{
if (getsubclasstable (mytreenode.id). ROWS.COUNT&GT;0)
{
DataTable mydatatable = new DataTable ();
mydatatable = getsubclasstable (mytreenode.id);

for (int i=0;i<mydatatable.rows.count;i++)
{
TreeNode mytreenodesub = new TreeNode ();

Mytreenodesub.checkbox=true;


Mytreenodesub.id = mydatatable.rows[i]["Subclass_code"]. ToString ();
mytreenodesub.expanded = false;
Mytreenodesub.target = "Body";
Mytreenodesub.text = mydatatable.rows[i]["Subclass_name"]. ToString ();
Mytreenodesub.navigateurl = "Doclist.aspx?" Doccatalogguid= "+ mydatatable.rows[i][" Class_code "]. ToString ();

MYTREENODE.NODES.ADD (mytreenodesub);

Appendchildren (mytreenodesub);

}
}
}





Private DataTable getclasstable ()
{
Binding Drpmc_code

String Strsql= "SELECT * from [class] ORDER by id DESC";

OpenConnection ();

SqlDataAdapter mycommand = new SqlDataAdapter (strsql,myconnection);
DataSet ds= new DataSet ();
Mycommand.fill (DS);

CloseConnection ();

Return DS. Tables[0];

}




Private DataTable getsubclasstable (string class_code)
{
Binding Drpmc_code

String Strsql= "select * from [subclass] where class_code= @class_code an ORDER by id DESC";

OpenConnection ();


SqlDataAdapter mycommand = new SqlDataAdapter (strsql,myconnection);

Mycommand.selectcommand.commandtype=commandtype.text;
MYCOMMAND.SELECTCOMMAND.PARAMETERS.ADD ("@class_code", sqldbtype.nvarchar,10). Value=class_code
;
DataSet ds= new DataSet ();
Mycommand.fill (DS);

CloseConnection ();

Return DS. Tables[0];

}




<summary>
Open a database connection
</summary>

private void OpenConnection ()
{

if (myConnection.State = = connectionstate.closed)
Myconnection.open ();

}





<summary>
To close a database connection
</summary>

private void CloseConnection ()
{

if (myConnection.State = = ConnectionState.Open)
Myconnection.close ();

}







}
}


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.