ASP. NET Treeview operation instance (Code debugging passed)

Source: Internet
Author: User

This instance completes the Treeview addition and modification function, and deletes the bound dropdownlist mobile function.

. Aspx code

<Form ID = "form1" method = "Post" runat = "server">
<Font face = "">
<Table id = "Table1" cellspacing = "1" cellpadding = "1"

Width = "700" border = "1">
<Tr>
<TD style = "width: 168px"> </TD>
<TD> Treeview operation instance </TD>
</Tr>
<Tr>
<TD style = "width: 168px" valign = "TOP">
<Iewc: Treeview id = "treeview1"

Runat = "server"> </iewc: Treeview> </TD>
<TD valign = "TOP">
<Table id = "Table2" cellspacing = "1"

Cellpadding = "1" width = "100%" border = "0">
<Tr>
<TD> node name
<Asp: textbox

Id = "textbox1" runat = "server"> </ASP: textbox> </TD>
</Tr>
<Tr>
<TD>
<Asp: button

Id = "addbutton" runat = "server" text = ""> </ASP: button>
<Asp: button

Id = "editbutton" runat = "server" text = "modify"> </ASP: button>
<Asp: button

Id = "delbutton" runat = "server" text = "delete"> </ASP: button> </TD>
</Tr>
<Tr>
<TD> mobile nodes </TD>
</Tr>
<Tr>
<TD> the node to be moved

<Asp: dropdownlist id = "dropdownlist1" runat = "server"> </ASP: dropdownlist> </TD>
</Tr>
<Tr>
<TD>
<Asp: button

Id = "movebutton" runat = "server" text = "mobile"> </ASP: button> </TD>
</Tr>
</Table>
</TD>
</Tr>
<Tr>
<TD style = "width: 168px"> </TD>
<TD> </TD>
</Tr>
</Table>
</Font>
</Form>

. Aspx. CS code

Using Microsoft. Web. UI. webcontrols;
Private void page_load (Object sender, system. eventargs E)
{
// Place user code here to initialize the page
If (! Page. ispostback ){
Button del = new button ();
Del = (button) page. findcontrol ("delbutton ");
Del. Attributes. Add ("onclick", "Return confirm ('Confirm delete ');");
Showlist ();
}
}
Private void showlist (){
// Bind the Treeview
// Infobase IO = new Infobase ();
Dropdownlist1.items. Clear ();
Listitem list = new listitem ();
List. value = "0 ";
List. Text = "all categories ";
Dropdownlist1.items. Add (list );

// Add the root node
Treeview1.nodes. Clear ();
Treenode node = new treenode ();
Node. Text = "category list ";
Node. Target = "0 ";
Node. Expanded = true;
Treeview1.nodes. Add (node );
Treeshow (0, node );
Treeview1.databind ();
}

Private void treeshow (INT parterid, treenode) {// recursive category
//
Dataset DS = returndataset (parterid );
If (Ds! = NULL)
{
For (INT I = 0; I <Ds. Tables [0]. Rows. Count; I ++)
{
Treenode node = new treenode ();
Node. Target = Ds. Tables [0]. Rows [I] ["ID"]. tostring ();
Node. Text = Ds. Tables [0]. Rows [I] ["name"]. tostring ();
Treenode. nodes. Add (node );
Node. Expanded = true;
Dataset ds2 = returndataset (int32.parse (Ds. Tables

[0]. Rows [I] ["ID"]. tostring ()));
If (ds2.tables [0]. Rows. Count> 0)
{
Treeshow (int32.parse (Ds. Tables [0]. Rows [I]

["ID"]. tostring (), node );
}
}
}
}

Private dataset returndataset (INT parterid ){
Oledbconnection conn = new oledbconnection ();
Conn. connectionstring = "provider = Microsoft. Jet. oledb.4.0; Data

Source = "+ server. mappath (" data. mdb ");
Oledbdataadapter cmd;
Dataset DS = new dataset ();
Try
{
Conn. open ();
Cmd = new oledbdataadapter ("select * From Treeview where

Parterid = "+ parterid +" order by createtime ASC ", Conn );
Cmd. Fill (DS );
}
Catch (exception ex)
{
Response. Write (ex. tostring ());
Response. End ();
}
Finally {
Conn. Dispose ();
}
Return Ds;
}

Private void addbutton_click (Object sender, system. eventargs E)
{// Add a node
If (textbox1.text! = "")
{
Treenode = treeview1.getnodefromindex

(Treeview1.selectednodeindex );
String [] treedepth = treenode. getnodeindex (). Split ('.');
If (treenode. Target! = NULL)
{
Int depth = treedepth. Length-1; // obtain the depth
Int parterid = int32.parse (treenode. Target); // obtain the parent class

ID
String classname = textbox1.text; // obtain the class name
Oledbconnection conn = new oledbconnection ();

Conn. connectionstring = "provider = Microsoft. Jet. oledb.4.0; Data Source =" + server. mappath

("Data. mdb ");
Conn. open ();
Oledbcommand cmd = new oledbcommand ();
Cmd. commandtype = commandtype. text;
Cmd. Connection = conn;
Cmd. commandtext = "insert into Treeview

(Name, parterid, depth) values ('"+ classname +"', "+ parterid +", "+ depth + ")";
Try
{
Cmd. executenonquery ();
Response. Write ("<SCRIPT> alert ('add

'); Location. href = 'treeviewstudy. aspx'; </SCRIPT> ");
Response. End ();
}
Catch (exception ex)
{
Response. Write (ex. tostring ());
Response. End ();
}
Finally
{
Cmd. Dispose ();
Conn. Dispose ();
}
}
Else {
// Response. Write (treenode. Target + "123 <br> ");
}
}
Else {
Response. Write ("enter a category and select a node ");
}
}

Private void editbutton_click (Object sender, system. eventargs E)
{// Modify a node
If (textbox1.text! = "")
{
Treenode = treeview1.getnodefromindex

(Treeview1.selectednodeindex );
Int id = int32.parse (treenode. Target );
String classname = textbox1.text;
If (treenode. Target! = NULL)
{
Oledbconnection conn = new oledbconnection ();

Conn. connectionstring = "provider = Microsoft. Jet. oledb.4.0; Data Source =" + server. mappath

("Data. mdb ");
Conn. open ();
Oledbcommand cmd = new oledbcommand ();
Cmd. commandtype = commandtype. text;
Cmd. Connection = conn;
Cmd. commandtext = "Update Treeview set

Name = '"+ classname +" 'where id = "+ ID;
Try
{
Cmd. executenonquery ();
Response. Write ("<SCRIPT> alert ('modified

'); Location. href = 'treeviewstudy. aspx'; </SCRIPT> ");
Response. End ();
}
Catch (exception ex)
{
Response. Write (ex. tostring ());
Response. End ();
}
Finally
{
Cmd. Dispose ();
Conn. Dispose ();
}
}
Else {}
}
Else
{
Response. Write ("enter a category and select a node ");
}
}

Private void delbutton_click (Object sender, system. eventargs E)
{// Delete
Treenode = treeview1.getnodefromindex

(Treeview1.selectednodeindex );
Int id = int32.parse (treenode. Target );
If (treenode. Target! = "0 ")
{
}
Else {
Response. Write ("root node cannot be deleted ");
}
}

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.