(转)多级树行控件加载
多级树行控件加载
1、首先下载microsoft 微软公司的iewebcontrols.msi树形控件,安装后,在visual studio .net
中的工具箱里“添加/移除项”中的“.NET Framework组件”选取添加“treeview控件”.
2、在项目引用中引用"Microsoft.Web.UI.WebControls"控件。
3、使用树形控件HTML相关语言
4、using Microsoft.Web.UI.WebControls; 加入上述引用,不然会出现编译错误:找不到类型或命名空间名称“TreeNode”(是否缺少 using 指令或程序集引用?)
Register TagPrefix="iewc" Namespace="Microsoft.Web.UI.WebControls" Assembly="Microsoft.Web.UI.WebControls, Version=1.0.2.226" %>
<iewc:treeview id="TreeView1" runat="server" ImageUrl="close.gif" ExpandedImageUrl="open.gif" EnableClientScript="false"></iewc:treeview>
<tr><td><asp:label id="lbltypename" runat="server" Text="名称:"></asp:label></td>
<td><asp:textbox id="tbName" runat="server"></asp:textbox></td>
</tr>
<tr><td><asp:button id="btnAdd" runat="server" Text="添加"></asp:button>
<asp:button id="btnUpdate" runat="server" Text="修改"></asp:button>
<asp:button id="btnDelete" runat="server" Width="48px" Text="删除"></asp:button>
</td>
</tr>
<asp:label id="lbID" runat="server" Visible="False"></asp:label> 为了记录父项的ID
4、.cs代码
private void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面
if (!this.IsPostBack)
{
//初始化连接字符串
this.lbID.Text="1";
conn.Open();
SqlDataAdapter adp = new SqlDataAdapter("select * from tbTree order by ID",conn);
DataSet ds=new DataSet();
adp.Fill(ds);
this.ViewState["ds"]=ds;
//调用递归函数,完成树形结构的生成
AddTree(1, (TreeNode)null);
}
}
//递归添加树的节点
public void AddTree(int ParentID,TreeNode pNode)
{
DataSet ds=(DataSet) this.ViewState["ds"];
DataView dvTree = new DataView(ds.Tables[0]);
//过滤ParentID,得到当前的所有子节点
dvTree.RowFilter = "[PARENTID] = " + ParentID;
foreach(DataRowView Row in dvTree)
{
TreeNode Node=new TreeNode() ;
if(pNode == null)
{ //添加根节点
Node.Text = Row["ConText"].ToString();
Node.ID= Row["ID"].ToString();
TreeView1.Nodes.Add(Node);
Node.Expanded=false;
AddTree(Int32.Parse(Row["ID"].ToString()), Node); //再次递归
}
else
{ //?添加当前节点的子节点
Node.Text = Row["ConText"].ToString();
Node.ID= Row["ID"].ToString();
pNode.Nodes.Add(Node);
Node.Expanded = false;
AddTree(Int32.Parse(Row["ID"].ToString()),Node); //再次递归
}
}
}
//选取节点的值
private void TreeView1_SelectedIndexChange(object sender, Microsoft.Web.UI.WebControls.TreeViewSelectEventArgs e)
{
this.tbName.Text =this.TreeView1.GetNodeFromIndex(this.TreeView1.SelectedNodeIndex).Text;
this.lbID.Text=this.TreeView1.GetNodeFromIndex(this.TreeView1.SelectedNodeIndex).ID;
}
//更新
private void btnUpdate_Click(object sender, System.EventArgs e)
{
string sql = "update tbTree set Context='"+this.tbName.Text+"' where ID='"+this.lbID.Text.Replace("'", "''").Trim()+"'";
SqlCommand mycommand = new SqlCommand(sql,conn);
conn.Open();
mycommand.ExecuteNonQuery();
conn.Close();
//调用递归函数,完成树形结构的生成
this.Response.Redirect("TreeRole.aspx");
this.tbName.Text="";
}
//删除相关节点信息
private void btnDelete_Click(object sender, System.EventArgs e)
{
string stringdelete="delete from tbTree where ID='"+this.lbID.Text.Replace("'", "''").Trim()+"'";
SqlCommand inst=new SqlCommand(stringdelete,conn);
conn.Open();
inst.ExecuteNonQuery();
conn.Close();
this.Response.Write(" <script language=javascript>alert('删除成功!');</script>");
//调用递归函数,完成树形结构的生成
this.Response.Redirect("TreeRole.aspx");
this.tbName.Text="";
}
//新增节点信息
private void btnAdd_Click(object sender, System.EventArgs e)
{
string sql="insert into tbTree(Context,ParentID) values('"+this.tbName.Text.Replace("'", "''").Trim() + "','"+this.lbID.Text.Replace("'", "''").Trim()+"')";
SqlCommand myCmd=new SqlCommand(sql,conn);
conn.Open();
myCmd.ExecuteNonQuery();
conn.Close();
//调用递归函数,完成树形结构的生成
this.Response.Redirect("TreeRole.aspx");
this.tbName.Text="";
}
5、数据表tbtree ID(int自动标识),Context(Nvarchar),ParentID(int)
其中子项的ParentID是父项的ID,这样循环加载多级时比较清晰。