asp.net treeView dynamically add nodes, edit nodes, delete nodes

Source: Internet
Author: User
Tags foreach instance method trim

<%
/*
This is an ASP tutorial. NET TreeView Dynamic increase node, edit node, delete node function, the following our first example is to add a single function of the node, later is the specific example is OH TreeView dynamically add nodes, edit nodes, delete node functions.
*/
Display data modification Save in TreeView node Seletedindexchange
protected void Treeview1_selectednodechanged (object sender, EventArgs e)
{
if (This.treeview1.selectednode!= null)
{
String xtbh = This.treeview1.selectednode.value.trim ();
if (This.treeview1.selectednode.value.trim ()!= "0")
{
This.hd_xtbh.value = XTBH;
DataSet ds = This.getjgxx (XTBH, false);
if (Ds.tables[0].rows.count > 0)
{
Txtbmmc.text = ds.tables[0].rows[0]["JIGMC"].tostring ();
Txtbmbh.text = ds.tables[0].rows[0]["JIGBH"].tostring ();
}
}
Else
{
Txtbmbh.text = "";
Txtbmmc.text =treeview1.selectednode.text.trim ();
}
}
}

Instance method
using System;
Using System.Collections;
Using System.Configuration;
Using System.Data;
Using System.Linq;
Using System.Web;
Using System.Web.Security;
Using System.Web.UI;
Using System.Web.UI.HtmlControls;
Using System.Web.UI.WebControls;
Using System.web.ui.webcontrols.webparts;
Using System.Xml.Linq;
Using Mycontent.model;
Using MYCONTENT.BLL;
Using System.Collections.Generic;

public partial class Admin_dianpu_dianputypemanager:system.web.ui.page
{
protected void Page_Load (object sender, EventArgs e)
{
if (!ispostback)
{
if (request.querystring.count!= 0)
{
int id = Convert.ToInt32 (request.querystring["id"].tostring ());
leixing TP = Leixingmanager.getleixingbyid (ID);
This.txtname.text = Tp.tname;
}
Bindtypebig ();
Showtreeview ();
}
}
Fill DropDownList Industry Type
public void Bindtypebig ()
{
ilist<leixing> Comps tutorial = Leixingmanager.getallleixingbytid (0);
This.ddlalltype.items.add ("Please choose");
foreach (Leixing compt in comps)
{
ListItem item = new ListItem ();
Item.value = Compt.tid.tostring ();
Item.text = Compt.tname;
This.ddlalltype.items.add (item);
}
}
Fill Treevview
public void Showtreeview ()
{
ilist<leixing> lists = Leixingmanager.getallleixingbytid (0);
foreach (leixing item in lists)
{
Create parent Node
TreeNode father = This.createtreenode (Item.tname, item.tid.tostring (), "dianputypemanager.aspx?id=" + Item.tid, " Images/opentree.gif ");
Createchildnode (Item.tid, Father);
This.tvproducttype.nodes.add (father);
}
}
Creating child nodes
public void Createchildnode (int typeid, TreeNode Fathernode)
{
ilist<leixing> lists = Leixingmanager.getallleixingbytid (typeid);
foreach (leixing item in lists)
{
TreeNode Child = This.createtreenode (Item.tname, item.tid.tostring (), "dianputypemanager.aspx?id=" + Item.tid, "images" /opentree.gif ");
Createchildnode (Item.tid, child);
Addtree (Fathernode, child);
}
}
The handle node joins the parent node
private void Addtree (TreeNode father, TreeNode Child)
{
Father.childnodes.add (child);
}
To create a node for a tree
Public TreeNode Createtreenode (string name, string id, string URL, string img)
{
TreeNode node = new TreeNode ();
Node.text = name;
Node.value = ID;
Node.navigateurl = URL;
Node.imageurl = img;
return node;
}
protected void Btnupdate_click (object sender, EventArgs e)
{
if (This.ddlalltype.text = "Please select")
{
if (This.txtname.text.trim () = "")
{
Scriptmanager.registerclientscriptblock (page, This.gettype (), "", "alert (' Modify, type name cannot be empty!") '), window.location= ' dianputypemanager.aspx ' ", true);
Response.Write ("<script> Alert" (' Modify, type name cannot be empty!) '); location= ' dianputypemanager.aspx ';</script> ');
}
else if (This.txtname.text.trim ()!= "")
{
int id = Convert.ToInt32 (request.querystring["id"].tostring ());//Get Selected Node ID
if (Leixingmanager.updateleixingbynameandid (This.txtname.text.trim (). ToString (), id) > 0)
{
Scriptmanager.registerclientscriptblock (page, This.gettype (), "", "Alert (' modified successfully! '), window.location= ' dianputypemanager.aspx ' ", true);
Response.Write ("<script> Alert" (' Modified successfully! '); location= ' dianputypemanager.aspx ';</script> ');
}
Else
{
Scriptmanager.registerclientscriptblock (page, This.gettype (), "", "alert (' Modify failed! '), window.location= ' dianputypemanager.aspx ' ", true);
Response.Write ("<script> Alert" (' Revision failed!) '); location= ' dianputypemanager.aspx ';</script> ');
}
}
}
Else
{
if (This.txtname.text.trim () = "")
{
Scriptmanager.registerclientscriptblock (page, This.gettype (), "", "alert (' Modify, type name cannot be empty!") '), window.location= ' dianputypemanager.aspx ' ", true);
Response.Write ("<script> Alert" (' Modify, type name cannot be empty!) '); location= ' dianputypemanager.aspx ';</script> ');
}
else if (This.txtname.text.trim ()!= "")
{
int id = Convert.ToInt32 (request.querystring["id"].tostring ());//Get Selected Node ID
int parentid = Convert.ToInt32 (this.ddlalltype.selectedvalue.tostring ());
if (Leixingmanager.updateleixingbyparentidandid (ParentID, id) > 0)
{
Scriptmanager.registerclientscriptblock (page, This.gettype (), "", "Alert (' modified successfully '), window.location= ' Dianputypemanager.aspx ' ", true);
Response.Write ("<script> Alert" (' Modified successfully! '); location= ' dianputypemanager.aspx ';</script> ');
}
Else
{
Scriptmanager.registerclientscriptblock (page, This.gettype (), "", "alert (' Modify failed! '), window.location= ' dianputypemanager.aspx ' ", true);
Response.Write ("<script> Alert" (' Revision failed!) '); location= ' dianputypemanager.aspx ';</script> ');
}
}
}


}
protected void Btndelete_click (object sender, EventArgs e)
{
if (request.querystring["id"].tostring () = null)
{
Scriptmanager.registerclientscriptblock (page, This.gettype (), "", "alert (' Delete: Please select Delete Item!") '), window.location= ' dianputypemanager.aspx ' ", true);
Response.Write (' <script> alert (' Delete: Please select Delete Item! '); location= ' dianputypemanager.aspx ';</script> ');
}
Else
{
int id = Convert.ToInt32 (request.querystring["id"].tostring ());//Get Selected Node ID
if (Leixingmanager.getallleixingbytid (ID). Count!= 0)
{
Scriptmanager.registerclientscriptblock (page, This.gettype (), "", "alert (' See RELATED links, then delete '), window.location= ' Dianputypemanager.aspx ' ", true);
Response.Write (' Please check the relevant links, then delete '); location= ' dianputypemanager.aspx ';</script> ');
}
Else
{
if (Leixingmanager.deleteleixingbyid (ID) > 0)
{
Scriptmanager.registerclientscriptblock (page, This.gettype (), "", "alert (' Delete succeeded! '), window.location= ' dianputypemanager.aspx ' ", true);
Response.Write (' <script> alert (' Delete succeeded! '); location= ' dianputypemanager.aspx ';</script> ');
}
Else
{
Scriptmanager.registerclientscriptblock (page, This.gettype (), "", "alert (' Delete failed! '), window.location= ' dianputypemanager.aspx ' ", true);
Response.Write (' <script> alert (' Delete failed! '); location= ' dianputypemanager.aspx ';</script> ');
}
}
}

}

}

%>

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.