Dynamically loading Dtree.js tree TreeView (Sample code) _javascript Tips

Source: Internet
Author: User

Copy Code code as follows:

<! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 strict//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd" >
<title>Destroydrop»Javascripts»Tree</title>
<link rel= "StyleSheet" href= "Dtree.css" type= "Text/css"/>
<script type= "Text/javascript" src= "Dtree.js" ></script>
<body>

<div class= "Dtree" >
<p><a href= "Javascript:d.openall ();" >open all</a> | <a href= "Javascript:d.closeall ();" >close all</a></p>
<script type= "Text/javascript" >
<!--
D = new Dtree (' d ');
D.add (0,-1, ' My example tree ');
D.add (1,0, ' Node 1 ', ' example01.html ');
D.add (2,0, ' Node 2 ', ' example01.html ');
D.add (3,1, ' Node 1.1 ', ' example01.html ');
D.add (4,0, ' Node 3 ', ' example01.html ');
D.add (5,3, ' Node 1.1.1 ', ' example01.html ');
D.add (6,5, ' Node 1.1.1.1 ', ' example01.html ');
D.add (7,0, ' Node 4 ', ' example01.html ');
D.add (8,1, ' Node 1.2 ', ' example01.html ');
D.add (9,0, ' My Pictures ', ' example01.html ', ' Pictures I ' ve taken over the years ', ', ', ', ' img/imgfolder.gif ');
D.add (10,9, ' The trip to Iceland ', ' example01.html ', ' Pictures of Gullfoss and Geysir ');
D.add (11,9, ' Mom ' s birthday ', ' example01.html ');
D.add (12,0, ' recycle Bin ', ' example01.html ', ', ', ', ' img/trash.gif ');
document.write (d);
-->
</script>
</div>
<p></p>
</body>


And then we can analyze what we're going to do if we want him to build dynamically.

We can see that the following code is the key to spanning a tree:

Copy Code code as follows:

<div class= "Dtree" >
<p><a href= "Javascript:d.openall ();" >open all</a> | <a href= "Javascript:d.closeall ();" >close all</a></p>
<script type= "Text/javascript" >
<!--
D = new Dtree (' d ');
D.add (0,-1, ' My example tree ');
D.add (1,0, ' Node 1 ', ' example01.html ');
D.add (2,0, ' Node 2 ', ' example01.html ');
D.add (3,1, ' Node 1.1 ', ' example01.html ');
D.add (4,0, ' Node 3 ', ' example01.html ');
D.add (5,3, ' Node 1.1.1 ', ' example01.html ');
D.add (6,5, ' Node 1.1.1.1 ', ' example01.html ');
D.add (7,0, ' Node 4 ', ' example01.html ');
D.add (8,1, ' Node 1.2 ', ' example01.html ');
D.add (9,0, ' My Pictures ', ' example01.html ', ' Pictures I ' ve taken over the years ', ', ', ', ' img/imgfolder.gif ');
D.add (10,9, ' The trip to Iceland ', ' example01.html ', ' Pictures of Gullfoss and Geysir ');
D.add (11,9, ' Mom ' s birthday ', ' example01.html ');
D.add (12,0, ' recycle Bin ', ' example01.html ', ', ', ', ' img/trash.gif ');
document.write (d);
-->
</script>
</div>

Can we write the above code into the. cs file? It's okay.

First we have to create a table in the database

Let's see, actually, there's so many table fields here. Personal feel PID child node (if is 0 then is root node, 1 child node, 3 child node child node) name to display name URL connection address title icon close the path of the picture Iconopen Open the path of the picture here is not detailed We can try our most important thing is to judge according to username, you can also be a reference to the username of the field I'm just a test here, so I used a string type. Below we'll load the TreeView dynamically

First we set up a database connection class to return a dataset type I'm a person. You can also return directly to a DataTable.

Copy Code code as follows:

Private DataSet Getdt (string username)
{
String strconn= "server=zhouyun;database=mytest;uid=sa;pwd=123456";
SqlConnection conn = new SqlConnection (strconn);
String strSQL = String. Format ("
SELECT * from TreeView where Username= ' {0} ', username);
SqlDataAdapter da = new SqlDataAdapter (strSQL, Conn);
DataSet ds = new DataSet ();
Da. Fill (DS);
return DS;

}


Then we put the first generation of the book JS into the. cs file The key one is to pay attention to. js file poured and. css file poured we can use the page.hearder.innerhtml= "" you want to pour the filename

Let's look at the code.

Copy Code code as follows:

public void TreeView ()
{
Strength of a long string type
StringBuilder Strbu = new StringBuilder ();
We are generating a class that retains the position of the static text
Literal lit = new Literal ();
Here's what we've just mentioned. to pour the. js and. cs files into place
Page. Header.innerhtml means putting this file reference into the HTML tag.
Page.Header.InnerHtml = @ "<
Link rel= ' StyleSheet ' href= ' dtree.css ' type= ' text/css '/>
<script type= ' text/javascript ' src= ' dtree.js ' ></script> '/pour over

String username = "Zhangsan";
DataSet ds = GETDT (username. Trim ());
DataTable dt = ds. Tables[0];
We're going to start putting JS files into long string types.

Strbu. Append (@ "<div class= ' Dtree ' >
<p><a href= ' Javascript:d.openall (); ' >open all</a> |
<a href= ' Javascript:d.closeall (); ' >close all</a></p>
<script type= ' Text/javascript ' >
<!--
D = new Dtree (' d ');
D.add (0,-1, ' My example tree '); /Here end a long character wear type
We start the Loop DataTable.
for (int i = 1; i < dt. Rows.Count; i++)
{
We're starting to add long file types Everybody here notice the stitching character worn
, be careful.
Strbu. Append (@ "
D.add ("+ i +", "+ dt.") rows["pid" + ", '" +
Dt. rows["name" + "', '" +
Dt. rows["url" + "', '" +
Dt. rows["target" + "', '" +
Dt. rows["icon"] + "', '" +
Dt. rows["Iconopen"]+ ");
}
Add long file type again
Strbu. Append (@ "document.write (d);
-->
</script>
</div> "); Long file type add end
Now we keep the text value of the static literal equal to the value of the long file type we just had
Lit. Text = convert.tostring (Strbu);
So we're wondering how I can put a piece of text in the <body>
<form> What about this position </form><boy>
Form1. InnerHtml = "";
We can get Form1 directly from the. cs file and then. Controls.Add ()
He can only put static text, so we assigned the value of the long file type to static text.
Then we can just add it here.
Form1. Controls.Add (lit);
}


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.