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>");
}
}

 

posted @ 2011-11-25 15:49  日月当空  阅读(2239)  评论(0编辑  收藏  举报