详细的Hashtable方法treeview遍历请参阅:
http://www.cnblogs.com/vegaslee/archive/2008/04/17/1158321.html
现主要分享如何将父结点的链接改为折叠事件.
看一下treeview页面的客户端源文件,可以发现可折叠的图片事件:TreeView_ToggleNode(...):
<a id="TreeView1n1"
href="javascript:TreeView_ToggleNode(TreeView1_Data,1,TreeView1n1,' ',TreeView1n1Nodes)">
<img src="/ass/WebResource.axd?d=r0O26jMIZhZAgGcswf2b2tbV9agiSKO716_OUUGC4hI1&t=633468101971007218"
alt="折叠 社团申请" style="border-width:0;" />
</a>
href="javascript:TreeView_ToggleNode(TreeView1_Data,1,TreeView1n1,' ',TreeView1n1Nodes)">
<img src="/ass/WebResource.axd?d=r0O26jMIZhZAgGcswf2b2tbV9agiSKO716_OUUGC4hI1&t=633468101971007218"
alt="折叠 社团申请" style="border-width:0;" />
</a>
这回了解了吧?只要在后台,将图片后面的超链接加上这个js脚本就可以了。
代码:
void hashTable()
{
DataTable dtregion = GetTreeTable();
Hashtable httree = new Hashtable();
foreach (DataRow myrow in dtregion.Rows)
{
TreeNode newnode = new TreeNode();
newnode.Value = myrow["ID"].ToString();
newnode.Text = myrow["TableName"].ToString();
newnode.Target = myrow["Target"].ToString();
newnode.NavigateUrl = myrow["url"].ToString();
newnode.ImageToolTip = myrow["ParentID"].ToString();
newnode.Expanded = bool.Parse(myrow["expanded"].ToString());
httree.Add(newnode.Value, newnode);
}
foreach (DataRow treerow in dtregion.Rows)
{
TreeNode newnode = (TreeNode)httree[treerow["ID"].ToString()];
TreeNode parentnode = (TreeNode)httree[newnode.ImageToolTip];
if (parentnode != null)
{
parentnode.ChildNodes.Add(newnode);
}
else
{
this.TreeView1.Nodes.Add(newnode);
}
}
AddToggle(this.TreeView1.Nodes[0]);
}
{
DataTable dtregion = GetTreeTable();
Hashtable httree = new Hashtable();
foreach (DataRow myrow in dtregion.Rows)
{
TreeNode newnode = new TreeNode();
newnode.Value = myrow["ID"].ToString();
newnode.Text = myrow["TableName"].ToString();
newnode.Target = myrow["Target"].ToString();
newnode.NavigateUrl = myrow["url"].ToString();
newnode.ImageToolTip = myrow["ParentID"].ToString();
newnode.Expanded = bool.Parse(myrow["expanded"].ToString());
httree.Add(newnode.Value, newnode);
}
foreach (DataRow treerow in dtregion.Rows)
{
TreeNode newnode = (TreeNode)httree[treerow["ID"].ToString()];
TreeNode parentnode = (TreeNode)httree[newnode.ImageToolTip];
if (parentnode != null)
{
parentnode.ChildNodes.Add(newnode);
}
else
{
this.TreeView1.Nodes.Add(newnode);
}
}
AddToggle(this.TreeView1.Nodes[0]);
}
private int totalCount=0;
private void AddToggle(TreeNode rootNode)
{
int nodeCount = 0;
//如果是父结点,加折叠事件
if (rootNode.ChildNodes.Count > 0)
{
int id = totalCount;
string para2 = "TreeView1n" + id.ToString();
string para3 = "TreeView1n" + id.ToString() + "Nodes";
rootNode.NavigateUrl = "javascript:TreeView_ToggleNode(TreeView1_Data," + id.ToString() + "," + para2 + ",' '," + para3 + ")";
}
//方法
while (rootNode.ChildNodes.Count > nodeCount)
{
totalCount++;
AddToggle(rootNode.ChildNodes[nodeCount]);
nodeCount++;
}
}
private void AddToggle(TreeNode rootNode)
{
int nodeCount = 0;
//如果是父结点,加折叠事件
if (rootNode.ChildNodes.Count > 0)
{
int id = totalCount;
string para2 = "TreeView1n" + id.ToString();
string para3 = "TreeView1n" + id.ToString() + "Nodes";
rootNode.NavigateUrl = "javascript:TreeView_ToggleNode(TreeView1_Data," + id.ToString() + "," + para2 + ",' '," + para3 + ")";
}
//方法
while (rootNode.ChildNodes.Count > nodeCount)
{
totalCount++;
AddToggle(rootNode.ChildNodes[nodeCount]);
nodeCount++;
}
}
相关文章
暂无评论...