asp.net treeView 动态增加节点、编辑节点、删除节点
<%
/*
这是一款asp教程.net treeview 动态增加节点、编辑节点、删除节点功能,下面我们第一个实例是讲增加节点的单一功能,后来是具体的举例说是哦treeview动态增加节点、编辑节点、删除节点功能吧。
*/
//treeview节点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();
}
}
}
//实例方法
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();
}
}
//填充dropdownlist行业类型
public void bindtypebig()
{
ilist<leixing> comps教程 = leixingmanager.getallleixingbytid(0);
this.ddlalltype.items.add("请选择");
foreach (leixing compt in comps)
{
listitem item = new listitem();
item.value = compt.tid.tostring();
item.text = compt.tname;
this.ddlalltype.items.add(item);
}
}
//填充treevview
public void showtreeview()
{
ilist<leixing> lists = leixingmanager.getallleixingbytid(0);
foreach (leixing item in lists)
{
//创建父节点
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);
}
}
//创建子节点
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);
}
}
//把子节点加入父节点
private void addtree(treenode father, treenode child)
{
father.childnodes.add(child);
}
//创建树的节点
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 == "请选择")
{
if (this.txtname.text.trim() == "")
{
scriptmanager.registerclientscriptblock(page, this.gettype(), "", "alert('修改时,类型名不能为空!'),window.location='dianputypemanager.aspx'", true);
//response.write("<script> alert('修改时,类型名不能为空!');location='dianputypemanager.aspx';</script>");
}
else if (this.txtname.text.trim() != "")
{
int id = convert.toint32(request.querystring["id"].tostring());//获取所选的节点id
if (leixingmanager.updateleixingbynameandid(this.txtname.text.trim().tostring(), id) > 0)
{
scriptmanager.registerclientscriptblock(page, this.gettype(), "", "alert('修改成功!'),window.location='dianputypemanager.aspx'", true);
//response.write("<script> alert('修改成功!');location='dianputypemanager.aspx';</script>");
}
else
{
scriptmanager.registerclientscriptblock(page, this.gettype(), "", "alert('修改失败!'),window.location='dianputypemanager.aspx'", true);
//response.write("<script> alert('修改失败!');location='dianputypemanager.aspx';</script>");
}
}
}
else
{
if (this.txtname.text.trim() == "")
{
scriptmanager.registerclientscriptblock(page, this.gettype(), "", "alert('修改时,类型名不能为空!'),window.location='dianputypemanager.aspx'", true);
//response.write("<script> alert('修改时,类型名不能为空!');location='dianputypemanager.aspx';</script>");
}
else if (this.txtname.text.trim() != "")
{
int id = convert.toint32(request.querystring["id"].tostring());//获取所选的节点id
int parentid = convert.toint32(this.ddlalltype.selectedvalue.tostring());
if (leixingmanager.updateleixingbyparentidandid(parentid, id) > 0)
{
scriptmanager.registerclientscriptblock(page, this.gettype(), "", "alert('修改成功'),window.location='dianputypemanager.aspx'", true);
//response.write("<script> alert('修改成功!');location='dianputypemanager.aspx';</script>");
}
else
{
scriptmanager.registerclientscriptblock(page, this.gettype(), "", "alert('修改失败!'),window.location='dianputypemanager.aspx'", true);
//response.write("<script> alert('修改失败!');location='dianputypemanager.aspx';</script>");
}
}
}
}
protected void btndelete_click(object sender, eventargs e)
{
if (request.querystring["id"].tostring() == null)
{
scriptmanager.registerclientscriptblock(page, this.gettype(), "", "alert('删除时:请选择删除项!'),window.location='dianputypemanager.aspx'", true);
//response.write("<script> alert('删除时:请选择删除项!');location='dianputypemanager.aspx';</script>");
}
else
{
int id = convert.toint32(request.querystring["id"].tostring());//获取所选的节点id
if (leixingmanager.getallleixingbytid(id).count != 0)
{
scriptmanager.registerclientscriptblock(page, this.gettype(), "", "alert('请查看相关链接,再行删除'),window.location='dianputypemanager.aspx'", true);
//response.write("<script>alert('请查看相关链接,再行删除');location='dianputypemanager.aspx';</script>");
}
else
{
if (leixingmanager.deleteleixingbyid(id) > 0)
{
scriptmanager.registerclientscriptblock(page, this.gettype(), "", "alert('删除成功!'),window.location='dianputypemanager.aspx'", true);
//response.write("<script> alert('删除成功!');location='dianputypemanager.aspx';</script>");
}
else
{
scriptmanager.registerclientscriptblock(page, this.gettype(), "", "alert('删除失败!'),window.location='dianputypemanager.aspx'", true);
//response.write("<script> alert('删除失败!');location='dianputypemanager.aspx';</script>");
}
}
}
}
}
%>
补充:asp.net教程,.Net开发