TreeView控件是APS.NET中的常用控件,现对其用法做一简单小结,具体内容如下:
1.public TreeNode FindNode(string valuePath) { ... } 其中valuePath指节点的值路径 示例代码如下:
/// <summary>
/// 填充左侧的模块树
/// </summary>
/// <param name="tv_modules">树形控件</param>
/// <param name="selectedMID">选中的模块组号</param>
public void BuildLeftTree(TreeView tv_modules, int selectedMID)
{
//添加根节点
TreeNode rootTN = new TreeNode();
rootTN.Text = SysConfig.LEFTTREE_ROOT_TEXT;
rootTN.Value = SysConfig.LEFTTREE_ROOT_VALUE;
rootTN.ImageUrl = SysConfig.LEFTTREE_ROOT_IMAGEURL;
rootTN.NavigateUrl = SysConfig.LEFTTREE_ROOT_NAVIGATEURL;
rootTN.Target = SysConfig.LEFTTREE_ROOT_TARGET;
rootTN.PopulateOnDemand = false;
rootTN.SelectAction = TreeNodeSelectAction.None;
rootTN.Expand();
tv_modules.Nodes.Add(rootTN);
PlatformAccessor pa = new PlatformAccessor();
DataSet ds = pa.getCodeRow(6, 1);
DataTable dt = ds.Tables[0];
DataRow dr = dt.Rows[0];
string content = dr["CONTENTS"].ToString();
XmlNodeList nodes = _xmldoc.SelectNodes("/Root/MyModules/Item");
if (content == "1")
{
//添加“我的模块”节点
TreeNode mymodulesTN = new TreeNode();
mymodulesTN.Text = SysConfig.LEFTTREE_MYMODULES_TEXT;
mymodulesTN.Value = SysConfig.LEFTTREE_MYMODULES_VALUE;
mymodulesTN.ImageUrl = SysConfig.LEFTTREE_MYMODULES_IMAGEURL;
mymodulesTN.NavigateUrl = SysConfig.LEFTTREE_MYMODULES_NAVIGATEURL;
mymodulesTN.Target = SysConfig.LEFTTREE_MYMODULES_TARGET;
mymodulesTN.PopulateOnDemand = false;
mymodulesTN.SelectAction = TreeNodeSelectAction.None;
mymodulesTN.Expand();
rootTN.ChildNodes.Add(mymodulesTN);
//添加“我的模块”
foreach (XmlNode xmlnode in nodes)
{
_addOneTreeNode(mymodulesTN, xmlnode, false);
}
}
PlatformAccessor pfa = new PlatformAccessor();
DataRow row = pfa.getModuleData(selectedMID);
int mlevel = int.Parse(row["MLEVEL"].ToString());
int mtopid = int.Parse(row["MTOPID"].ToString());
if (mlevel == 1 && mtopid == -1)
{
XmlNodeList rootList = _xmldoc.SelectNodes("/Root/Item");
foreach (XmlNode xn in rootList)
{
_addOneTreeNode(rootTN, xn, false, selectedMID);
}
return;
}
else
{
int mid = selectedMID;
int[] mids = new int[mlevel];
for (int i = 0; i < mlevel; i++)
{
DataRow dr_Temp = pfa.getModuleData(mid);
mids[i] = int.Parse(dr_Temp["MID"].ToString());
mid = int.Parse(dr_Temp["MTOPID"].ToString());
}
int[] sort_mids = new int[mlevel];
for (int i = 0; i < mlevel; i++)
{
sort_mids[i] = mids[mlevel - 1 - i];
}
for (int i = 0; i < mlevel; i++)
{
if (i == 0)
{
XmlNodeList rootList = _xmldoc.SelectNodes("/Root/Item");
foreach (XmlNode xn in rootList)
{
_addOneTreeNode(rootTN, xn, false, sort_mids[i]);
}
}
else
{
string temp = "";
for (int k = 0; k < (i + 1); k++)
{
temp += "/" + sort_mids[k].ToString();
}
//string valuePath = "ROOT/1031/1036";
string valuePath = SysConfig.LEFTTREE_ROOT_VALUE + temp;
TreeNode treeNode = tv_modules.FindNode(valuePath);
treeNode.Expand();
}
}
}
}