分类:
2009-11-29 18:24:59
添加一TreeView软件
<asp:TreeView ID="tvMenu" runat="server" ShowLines="True" ExpandDepth="0" Target="middle" Height="295px" OnLoad="tvMenu_Load" width="145px" BorderStyle="None" CollapseImageUrl="~/images/icon/folder.gif" ExpandImageUrl="~/images/icon/folderopen.gif" NoExpandImageUrl="~/images/icon/folder.gif" LineImagesFolder="~/TreeLineImages">
<ParentNodeStyle Font-Bold="False" ForeColor="Black" ImageUrl="../images/icon/folder.gif" />
<HoverNodeStyle Font-Underline="True" ForeColor="Blue" />
<SelectedNodeStyle Font-Underline="True" HorizontalPadding="0px" VerticalPadding="0px" ForeColor="Green" ImageUrl="~/images/icon/folder.gif" />
<NodeStyle Font-Names="Tahoma" Font-Size="8pt" ForeColor="DarkBlue" HorizontalPadding="5px" NodeSpacing="0px" VerticalPadding="0px" />
asp:TreeView>
OnLoad="tvMenu_Load",即先运动tvMenu_Load事件
protected void tvMenu_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
tvMenu.Dispose();
myco = new MySqlConnection(strconn);
myco.Open();
this.createDataSet();
myco.Close();
this.InitTree(tvMenu.Nodes, "0");
}
}
protected void InitTree(TreeNodeCollection Nds, string idmde) //用递归方法动态生成节点
{
DataView dv = new DataView();
TreeNode tmpNode;
dv.Table = ds.Tables["Tree"];
dv.RowFilter = "idmde=" + "'" + idmde + "'";
foreach (DataRowView drv in dv)
{
tmpNode = new TreeNode();
tmpNode.Value = drv["idmke"].ToString();
tmpNode.Text = drv["denam"].ToString();
tmpNode.NavigateUrl = drv["url"].ToString();
tmpNode.ImageUrl = "../images/icon/sms_type11.gif";
Nds.Add(tmpNode);
this.InitTree(tmpNode.ChildNodes, tmpNode.Value);
}
}
private DataSet createDataSet()
{
ds = new DataSet();
string sqlstr = "select * from tree_depart";
MySqlDataAdapter cmdSelect = new MySqlDataAdapter(sqlstr, myco);
cmdSelect.Fill(ds, "Tree");
//cmdSelect.Dispose();
//sqlstr = "select * from kepart";
//cmdSelect = new MySqlDataAdapter(sqlstr, myco);
//cmdSelect.Fill(ds, "kepart");
//cmdSelect.Dispose();
//sqlstr = "select * from zupart";
//cmdSelect = new MySqlDataAdapter(sqlstr, myco);
//cmdSelect.Fill(ds, "zupart");
return ds;
}
private MySqlCommand mycomm;
private MySqlDataReader dr;
private string strconn = ConfigurationManager.AppSettings["Mysqlstudb"];
private MySqlConnection myco;
DataSet ds;