TreeView 动态绑定数据及在当前节点操作

    protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
this.tvwList.Nodes.Clear();//绑定之前清除所有treeview里的节点数据
bindTree(this.tvwList.Nodes, "0");//调用绑定treeview节点方法
}
}

///<summary>
/// 修改当前节点
///</summary>
///<param name="sender"></param>
///<param name="e"></param>
protected void btnModify_Click(object sender, EventArgs e)
{
if (!string.IsNullOrEmpty(this.txtUpdate.Text))
{
ProductTypeSearchModel product = new ProductTypeSearchModel();
product.TypeName=this.txtUpdate.Text;
product.TypeId = Convert.ToInt32(this.tvwList.SelectedNode.Value);
if (ProductTypeSearchBLL.ModifyProductTypeSearch(product) > 0)
{
Response.Write("<script>alert('节点名称修改成功');window.location.href=window.location.href</script>");
}
else
{
Response.Write("<script>alert('节点名称修改失败')</script>");
}
}
else
{
Response.Write("<script>alert('节点名称不能为空')</script>");
}
}

///<summary>
/// 新增节点
///</summary>
///<param name="sender"></param>
///<param name="e"></param>
protected void btnAdd_Click(object sender, EventArgs e)
{
if (!string.IsNullOrEmpty(this.txtAdd.Text))
{
ProductTypeSearchModel product = new ProductTypeSearchModel();
product.TypeName = this.txtAdd.Text;
product.TypeParentId = Convert.ToInt32(this.tvwList.SelectedNode.Value);
if (ProductTypeSearchBLL.AddProductTypeSearch(product) > 0)
{
Response.Write("<script>alert('节点名称新增成功');window.location.href=window.location.href</script>");
}
else
{
Response.Write("<script>alert('节点名称新增失败')</script>");
}
}
else
{
Response.Write("<script>alert('节点名称不能为空')</script>");
}
}

///<summary>
/// 选择节点变化时
///</summary>
///<param name="sender"></param>
///<param name="e"></param>
protected void tvwList_SelectedNodeChanged(object sender, EventArgs e)
{
this.lblNow.Text = "当前选定节点为:" + this.tvwList.SelectedNode.Text;
this.txtUpdate.Text = this.tvwList.SelectedNode.Text;
}

///<summary>
/// 绑定treeview
///</summary>
///<param name="tnc">treeview的nodes</param>
///<param name="parentId">父ID</param>
protected void bindTree(TreeNodeCollection tnc, string parentId)
{
//创建DataView

DataView dview = new DataView();
dview.Table = ProductTypeSearchBLL.GetProductTypeSearch();

//加条件
dview.RowFilter = "TypeParentId='" + parentId + "'";
foreach (DataRowView rows in dview)
{
TreeNode tn = new TreeNode();
tn.Value = rows["TypeId"].ToString();
tn.Text = rows["TypeName"].ToString();

//如果实现点击节点文字展开收缩效果的话下面这句代码

//tn.SelectAction = TreeNodeSelectAction.Expand;
tnc.Add(tn);
//TreeView1.Nodes.Add(tn);
DataView dv = new DataView();
dv.Table = ProductTypeSearchBLL.GetProductTypeSearch();

//判断是不是存在drv["flbh"]的ChildNodes,存在的话添加,不存在就继续遍历

dv.RowFilter = "TypeParentId='" + rows["TypeId"].ToString() + "'";
if (dv.Count != 0)
{
bindTree(tn.ChildNodes, tn.Value);
}
}
}

///<summary>
/// 删除节点
///</summary>
///<param name="sender"></param>
///<param name="e"></param>
protected void btnDel_Click(object sender, EventArgs e)
{
int typeId = Convert.ToInt32(this.tvwList.SelectedNode.Value);
if (ProductTypeSearchBLL.DelProductTypeSearch(typeId) > 0)
{
Response.Write("<script>alert('节点名称删除成功');window.location.href=window.location.href</script>");
}
else
{
Response.Write("<script>alert('节点名称删除失败')</script>");
}
}

 

转载于:https://www.cnblogs.com/Kiss920Zz/archive/2011/11/25/2263374.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值