<form runat="server">
<asp:TreeView ID="TreeView1" runat="server">
</asp:TreeView>
</form>
public void InitTree(TreeView TreeView1)
{
DataSet dsa = new DataSet();
string sql = "SELECT id,name FROM M_menuhead";
dsa = SqlHelper.ExecuteDataset(SqlHelper.Conn, CommandType.Text, sql);
if (dsa.Tables[0].Rows.Count > 0)
{
for (int i = 0; i <= dsa.Tables[0].Rows.Count-1; i++)
{
//把目录表绑定到根节点,作为第一级节点
TreeNode TreeNode1 = new TreeNode();
TreeNode1.Value = dsa.Tables[0].Rows[i]["id"].ToString();
TreeNode1.Text = dsa.Tables[0].Rows[i]["name"].ToString();
TreeNode1.SelectAction = TreeNodeSelectAction.None;//选定节点,事件为空
//填充数据集:ds1
DataSet ds1 = new DataSet();
string sql2 = "SELECT id,name FROM M_MenuChildren where pid=" + dsa.Tables[0].Rows[i][0].ToString().Trim() + " ";
ds1 = SqlHelper.ExecuteDataset(SqlHelper.Conn, CommandType.Text, sql2);
if (ds1.Tables[0].Rows.Count > 0)
{
for (int j = 0; j < ds1.Tables[0].Rows.Count; j++)
{
//把模块表绑定到子节点,作为第二级节点
TreeNode TreeNode2 = new TreeNode();
TreeNode2.Value = ds1.Tables[0].Rows[j]["id"].ToString();
TreeNode2.Text = ds1.Tables[0].Rows[j]["name"].ToString();
TreeNode2.SelectAction = TreeNodeSelectAction.None; //选定节点,事件为空
//把模块名称绑定到目录名称下,作为子节点
TreeNode2.ExpandAll();
TreeNode1.ChildNodes.Add(TreeNode2);
}
}
// 把目录名称绑定到TreeView控件,作为父节点
TreeNode1.ExpandAll();
TreeView1.Nodes.Add(TreeNode1);
}
}
}
}
编译后的 treeview 产生很多代码
<form name="aspnetForm" method="post" action="index.aspx" id="aspnetForm">
<div>
<input type="hidden" name="menuhead2_TreeView1_ExpandState" id="menuhead2_TreeView1_ExpandState" value="ennnnnnnnnnnnn" />
<input type="hidden" name="menuhead2_TreeView1_SelectedNode" id="menuhead2_TreeView1_SelectedNode" value="" />
<input type="hidden" name="__EVENTTARGET" id="__EVENTTARGET" value="" />
<input type="hidden" name="__EVENTARGUMENT" id="__EVENTARGUMENT" value="" />
<input type="hidden" name="menuhead2_TreeView1_PopulateLog" id="menuhead2_TreeView1_PopulateLog" value="" />
<input type="hidden" name="__VIEWSTATE" id="__VIEWSTATE" value="/wEPDwUKLTc2ODkzNzUxNQ9kFgICAQ9kFgICAQ9kFgICAQ88KwAJAgAPFgYeDU5ldmVyRXhwYW5kZWRkHgxTZWxlY3RlZE5vZGVkHglMYXN0SW5kZXgCDmQIFCsABwUXMDowLDA6MSwwOjIsMDozLDA6NCwwOjUUKwACFggeBVZhbHVlBQExHgRUZXh0BQbmiYvmnLoeDFNlbGVjdEFjdGlvbgsqLlN5c3RlbS5XZWIuVUkuV2ViQ29udHJvbHMuVHJlZU5vZGVTZWxlY3RBY3Rpb24DHghFeHBhbmRlZGcUKwAJBR8wOjAsMDoxLDA6MiwwOjMsMDo0LDA6NSwwOjYsMDo3FCsAAhYIHwMFATEfBAUJ6K+65Z+65LqaHwULKwQDHwZnZBQrAAIWCB8DBQEyHwQFD+e0ouWwvOeIseeri+S/oR8FCysEAx8GZ2QUKwACFggfAwUBMx8EBQzmkanmiZjnvZfmi4kfBQsrBAMfBmdkFCsAAhYIHwMFATQfBAUG5LiJ5pifHwULKwQDHwZnZBQrAAIWCB8DBQE1HwQFAkxHHwULKwQDHwZnZBQrAAIWCB8DBQE2HwQFBuiBlOaDsx8FCysEAx8GZ2QUKwACFggfAwUBNx8EBQnopb/pl6jlrZAfBQsrBAMfBmdkFCsAAhYIHwMFATgfBAUG5YW25a6DHwULKwQDHwZnZBQrAAIWCB8DBQEyHwQFCeeslOiusOacrB8FCysEAx8GZ2QUKwACFggfAwUBMx8EBQznlLXohJHphY3ku7YfBQsrBAMfBmdkFCsAAhYIHwMFATQfBAUG5pyN6KOFHwULKwQDHwZnZBQrAAIWCB8DBQE1HwQFBueOqeWFtx8FCysEAx8GZ2QUKwACFggfAwUBNh8EBQblhbblroMfBQsrBAMfBmdkZBgBBR5fX0NvbnRyb2xzUmVxdWlyZVBvc3RCYWNrS2V5X18WAQUTbWVudWhlYWQyJFRyZWVWaWV3Mbe7eEArAbNzY7la3QPYpCiIblp+" />
</div>
<script type="text/javascript">
//<![CDATA[
var theForm = document.forms['aspnetForm'];
if (!theForm) {
theForm = document.aspnetForm;
}
function __doPostBack(eventTarget, eventArgument) {
if (!theForm.onsubmit || (theForm.onsubmit() != false)) {
theForm.__EVENTTARGET.value = eventTarget;
theForm.__EVENTARGUMENT.value = eventArgument;
theForm.submit();
}
}
//]]>
</script>
<script src="/B2C/WebResource.axd?d=D2ytoLOAacQa5-Nrzao1MQ2&t=633906213980000000" type="text/javascript"></script>
<script src="/B2C/WebResource.axd?d=0J8Ob1VlN6fpYpbxRCsA_Q2&t=633906213980000000" type="text/javascript"></script>
<script type="text/javascript">
//<![CDATA[
function TreeView_PopulateNodeDoCallBack(context,param) {
WebForm_DoCallback(context.data.treeViewID,param,TreeView_ProcessNodeData,context,TreeView_ProcessNodeData,false);
}
var menuhead2_TreeView1_Data = null;//]]>
</script>
<a href="#menuhead2_TreeView1_SkipLink"><img alt="跳过导航链接。" src="/B2C/WebResource.axd?d=oz5w1wGopV44fkNLinRmbQ2&t=633906213980000000" width="0" height="0" style="border-0px;" /></a><div id="menuhead2_TreeView1">
<table cellpadding="0" cellspacing="0" style="border-0;">
<tr>
<td><a id="menuhead2_TreeView1n0" href="javascript:TreeView_ToggleNode(menuhead2_TreeView1_Data,0,menuhead2_TreeView1n0,' ',menuhead2_TreeView1n0Nodes)"><img src="/B2C/WebResource.axd?d=zJOjpKHuzpIlaPQpjwHvS0d56KtK1vL2EljRG21fT2k1&t=633906213980000000" alt="折叠 手机" style="border-0;" /></a></td><td style="white-space:nowrap;"><span id="menuhead2_TreeView1t0" style="text-decoration:none;">手机</span></td>
</tr>
</table><div id="menuhead2_TreeView1n0Nodes" style="display:block;">
<table cellpadding="0" cellspacing="0" style="border-0;">
<tr>
<td><div style="20px;height:1px"></div></td><td><img src="/B2C/WebResource.axd?d=zJOjpKHuzpIlaPQpjwHvS0dhtiUqUNUklEDCNv-50ao1&t=633906213980000000" alt="" /></td><td style="white-space:nowrap;"><span id="menuhead2_TreeView1t1" style="text-decoration:none;">诺基亚</span></td>
</tr>
</table><table cellpadding="0" cellspacing="0" style="border-0;">
<tr>
<td><div style="20px;height:1px"></div></td><td><img src="/B2C/WebResource.axd?d=zJOjpKHuzpIlaPQpjwHvS0dhtiUqUNUklEDCNv-50ao1&t=633906213980000000" alt="" /></td><td style="white-space:nowrap;"><span id="menuhead2_TreeView1t2" style="text-decoration:none;">索尼爱立信</span></td>
</tr>
</table><table cellpadding="0" cellspacing="0" style="border-0;">
<tr>
<td><div style="20px;height:1px"></div></td><td><img src="/B2C/WebResource.axd?d=zJOjpKHuzpIlaPQpjwHvS0dhtiUqUNUklEDCNv-50ao1&t=633906213980000000" alt="" /></td><td style="white-space:nowrap;"><span id="menuhead2_TreeView1t3" style="text-decoration:none;">摩托罗拉</span></td>
</tr>
</table><table cellpadding="0" cellspacing="0" style="border-0;">
<tr>
<td><div style="20px;height:1px"></div></td><td><img src="/B2C/WebResource.axd?d=zJOjpKHuzpIlaPQpjwHvS0dhtiUqUNUklEDCNv-50ao1&t=633906213980000000" alt="" /></td><td style="white-space:nowrap;"><span id="menuhead2_TreeView1t4" style="text-decoration:none;">三星</span></td>
</tr>
</table><table cellpadding="0" cellspacing="0" style="border-0;">
<tr>
<td><div style="20px;height:1px"></div></td><td><img src="/B2C/WebResource.axd?d=zJOjpKHuzpIlaPQpjwHvS0dhtiUqUNUklEDCNv-50ao1&t=633906213980000000" alt="" /></td><td style="white-space:nowrap;"><span id="menuhead2_TreeView1t5" style="text-decoration:none;">LG</span></td>
</tr>
</table><table cellpadding="0" cellspacing="0" style="border-0;">
<tr>
<td><div style="20px;height:1px"></div></td><td><img src="/B2C/WebResource.axd?d=zJOjpKHuzpIlaPQpjwHvS0dhtiUqUNUklEDCNv-50ao1&t=633906213980000000" alt="" /></td><td style="white-space:nowrap;"><span id="menuhead2_TreeView1t6" style="text-decoration:none;">联想</span></td>
</tr>
</table><table cellpadding="0" cellspacing="0" style="border-0;">
<tr>
<td><div style="20px;height:1px"></div></td><td><img src="/B2C/WebResource.axd?d=zJOjpKHuzpIlaPQpjwHvS0dhtiUqUNUklEDCNv-50ao1&t=633906213980000000" alt="" /></td><td style="white-space:nowrap;"><span id="menuhead2_TreeView1t7" style="text-decoration:none;">西门子</span></td>
</tr>
</table><table cellpadding="0" cellspacing="0" style="border-0;">
<tr>
<td><div style="20px;height:1px"></div></td><td><img src="/B2C/WebResource.axd?d=zJOjpKHuzpIlaPQpjwHvS0dhtiUqUNUklEDCNv-50ao1&t=633906213980000000" alt="" /></td><td style="white-space:nowrap;"><span id="menuhead2_TreeView1t8" style="text-decoration:none;">其它</span></td>
</tr>
</table>
</div><table cellpadding="0" cellspacing="0" style="border-0;">
<tr>
<td><img src="/B2C/WebResource.axd?d=zJOjpKHuzpIlaPQpjwHvS0dhtiUqUNUklEDCNv-50ao1&t=633906213980000000" alt="" /></td><td style="white-space:nowrap;"><span id="menuhead2_TreeView1t9" style="text-decoration:none;">笔记本</span></td>
</tr>
</table><table cellpadding="0" cellspacing="0" style="border-0;">
<tr>
<td><img src="/B2C/WebResource.axd?d=zJOjpKHuzpIlaPQpjwHvS0dhtiUqUNUklEDCNv-50ao1&t=633906213980000000" alt="" /></td><td style="white-space:nowrap;"><span id="menuhead2_TreeView1t10" style="text-decoration:none;">电脑配件</span></td>
</tr>
</table><table cellpadding="0" cellspacing="0" style="border-0;">
<tr>
<td><img src="/B2C/WebResource.axd?d=zJOjpKHuzpIlaPQpjwHvS0dhtiUqUNUklEDCNv-50ao1&t=633906213980000000" alt="" /></td><td style="white-space:nowrap;"><span id="menuhead2_TreeView1t11" style="text-decoration:none;">服装</span></td>
</tr>
</table><table cellpadding="0" cellspacing="0" style="border-0;">
<tr>
<td><img src="/B2C/WebResource.axd?d=zJOjpKHuzpIlaPQpjwHvS0dhtiUqUNUklEDCNv-50ao1&t=633906213980000000" alt="" /></td><td style="white-space:nowrap;"><span id="menuhead2_TreeView1t12" style="text-decoration:none;">玩具</span></td>
</tr>
</table><table cellpadding="0" cellspacing="0" style="border-0;">
<tr>
<td><img src="/B2C/WebResource.axd?d=zJOjpKHuzpIlaPQpjwHvS0dhtiUqUNUklEDCNv-50ao1&t=633906213980000000" alt="" /></td><td style="white-space:nowrap;"><span id="menuhead2_TreeView1t13" style="text-decoration:none;">其它</span></td>
</tr>
</table>
</div><a id="menuhead2_TreeView1_SkipLink"></a>
<script type="text/javascript">
//<![CDATA[
var menuhead2_TreeView1_ImageArray = new Array('', '', '', '/B2C/WebResource.axd?d=zJOjpKHuzpIlaPQpjwHvS0dhtiUqUNUklEDCNv-50ao1&t=633906213980000000', '/B2C/WebResource.axd?d=zJOjpKHuzpIlaPQpjwHvS7W5h1k7La5Mti9gRoqwwHU1&t=633906213980000000', '/B2C/WebResource.axd?d=zJOjpKHuzpIlaPQpjwHvS0d56KtK1vL2EljRG21fT2k1&t=633906213980000000');
//]]>
</script>
<script type="text/javascript">
//<![CDATA[
WebForm_InitCallback();var menuhead2_TreeView1_Data = new Object();
menuhead2_TreeView1_Data.images = menuhead2_TreeView1_ImageArray;
menuhead2_TreeView1_Data.collapseToolTip = "折叠 {0}";
menuhead2_TreeView1_Data.expandToolTip = "展开 {0}";
menuhead2_TreeView1_Data.expandState = theForm.elements['menuhead2_TreeView1_ExpandState'];
menuhead2_TreeView1_Data.selectedNodeID = theForm.elements['menuhead2_TreeView1_SelectedNode'];
for (var i=0;i<6;i++) {
var preLoad = new Image();
if (menuhead2_TreeView1_ImageArray[i].length > 0)
preLoad.src = menuhead2_TreeView1_ImageArray[i];
}
menuhead2_TreeView1_Data.lastIndex = 14;
menuhead2_TreeView1_Data.populateLog = theForm.elements['menuhead2_TreeView1_PopulateLog'];
menuhead2_TreeView1_Data.treeViewID = 'menuhead2$TreeView1';
menuhead2_TreeView1_Data.name = 'menuhead2_TreeView1_Data';
//]]>
</script>
</form>