急 asp.net TreeView 动态数据绑定

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了急 asp.net TreeView 动态数据绑定相关的知识,希望对你有一定的参考价值。

请帮忙写个 TreeView 动态数据绑定 程序 我的数据库 no主键自动增长 menuitem 技术部张三 财务部李四 技术部王五 menuclass 技术部 财务部 业务部 技术部 menureport 菜单 菜单 menuflag 1 1 1 1 要详细的代码要注解

给你个例子,照着改改就可以用的
#region 多级别树形导航的处理,绑定第一层
private void PopulateCategories(string strTableName)

this.TVMenu.Nodes.Clear();
try

string strSQL = "select * from " + strTableName + " where SYS_FLD_CLASS_GRADE = 1";

KBaseServerDAL kal = new KBaseServerDAL();
RecordSet rs = kal.GetDataSetBySQL(strSQL);

if (rs != null && rs.GetCount() > 0)

for (int i = 0; i < rs.GetCount(); i++)

//string strField = rs.GetValue("SYS_FLD_CLASS_NAME");
TreeNode NewNode = new TreeNode("<a href = " + this.TextBox_Page.Text + "?sysCode=" + rs.GetValue("SYS_FLD_CLASS_NAME").ToString() + ">" + rs.GetValue("SYS_FLD_CLASS_NAME").ToString() + "</a>", "" + rs.GetValue("SYS_FLD_SYS_CODE").ToString() + "," + rs.GetValue("SYS_FLD_CLASS_GRADE").ToString() + "");
NewNode.PopulateOnDemand = true;

this.TVMenu.Nodes.Add(NewNode);
NewNode.CollapseAll();
rs.MoveNext();



kal.DisconnectServer();

catch

this.Label_Text.Text = "数据库连接有误,请检查配置。";


#endregion

#region 多级别树形导航的处理,单击树节点时的处理函数
protected void TVMenu_TreeNodeExpanded(object sender, TreeNodeEventArgs e)

try

//防止重复填充节点
if (e.Node.ChildNodes.Count > 0)
return;

string[] str = e.Node.Value.Split(',');
string strSysCode = str[0].ToString();
string strGrade = "";

strGrade = Convert.ToString((Convert.ToInt32(str[1]) + 1));

string strSQL = "select * from " + this.TextBox6.Text.Trim() + " where SYS_FLD_CLASS_GRADE = " + strGrade + " and SYS_FLD_SYS_CODE = '" + strSysCode + "?'";

KBaseServerDAL kal = new KBaseServerDAL();
RecordSet rs = kal.GetDataSetBySQL(strSQL);

if (rs != null && rs.GetCount() > 0)

for (int i = 0; i < rs.GetCount(); i++)

string strField = rs.GetValue("SYS_FLD_CLASS_NAME");
TreeNode NewNode = new TreeNode("<a href = " + this.TextBox_Page.Text + "?sysCode=" + rs.GetValue("SYS_FLD_CLASS_NAME").ToString() + ">" + rs.GetValue("SYS_FLD_CLASS_NAME").ToString() + "</a>", "" + rs.GetValue("SYS_FLD_SYS_CODE").ToString() + "," + rs.GetValue("SYS_FLD_CLASS_GRADE").ToString() + "");
NewNode.PopulateOnDemand = true;

e.Node.ChildNodes.Add(NewNode);
NewNode.CollapseAll();
rs.MoveNext();



kal.DisconnectServer();

catch

this.Label_Text.Text = "数据库连接有误,请检查配置。";


#endregion
参考技术A <script language="javascript" type="text/javascript">
<!--
function TreeView_ToggleNode(data, index, node, lineType, children)

var img = node.childNodes[0];
var newExpandState;
try
//***折叠兄弟节点(Collapse Brothers)-----
CollapseBrothers(data,children);
//---------------------------------------

if (children.style.display == "none")

children.style.display = "block";
newExpandState = "e";
if ((typeof(img) != "undefined") && (img != null))

if (lineType == "l")

img.src = data.images[15];

else if (lineType == "t")

img.src = data.images[12];

else if (lineType == "-")

img.src = data.images[18];

else

img.src = data.images[5];

img.alt = data.collapseToolTip.replace(/\0\/, TreeView_GetNodeText(node));


else

children.style.display = "none";
newExpandState = "c";
if ((typeof(img) != "undefined") && (img != null))

if (lineType == "l")

img.src = data.images[14];

else if (lineType == "t")

img.src = data.images[11];

else if (lineType == "-")

img.src = data.images[17];

else

img.src = data.images[4];

img.alt = data.expandToolTip.replace(/\0\/, TreeView_GetNodeText(node));



catch(e)
data.expandState.value = data.expandState.value.substring(0, index) + newExpandState + data.expandState.value.slice(index + 1);


//折叠兄弟节点(Collapse Brothers)
function CollapseBrothers(data,childContainer)

var parent = childContainer.parentNode;
for(i=0; i< parent.childNodes.length; i++)

if(parent.childNodes[i].tagName.toLowerCase() =="div")

if(parent.childNodes[i].id != childContainer.id)

parent.childNodes[i].style.display = "none"


else if(parent.childNodes[i].tagName.toLowerCase() =="table")

var treeLinks = parent.childNodes[i].getElementsByTagName("a");
if(treeLinks.length > 2)

var j=0;
if(treeLinks[j].firstChild.tagName)

if(treeLinks[j].firstChild.tagName.toLowerCase() == "img")

var img = treeLinks[j].firstChild;
if(i==0)
img.src = data.images[8];
else if(i==parent.childNodes.length-2)
img.src = data.images[14];
else
img.src = data.images[11];






-->
</script>
<asp:TreeView ID="TreeView1" runat="server" OnSelectedNodeChanged="TreeView1_SelectedNodeChanged" ShowLines="True">
<ParentNodeStyle Font-Bold="False" />
<HoverNodeStyle Font-Underline="True" ForeColor="maroon" />
<RootNodeStyle Font-Underline="True" />
<LeafNodeStyle ForeColor="#404000" />
<NodeStyle Font-Names="Verdana" Font-Size="8pt" Font-Underline="True" ForeColor="Black" HorizontalPadding="5px" NodeSpacing="0px" VerticalPadding="0px" />
<SelectedNodeStyle BackColor="LightYellow" Font-Bold="False" Font-Underline="True" ForeColor="Green" HorizontalPadding="0px" NodeSpacing="0px" VerticalPadding="0px" />
</asp:TreeView>

protected void bind_trade(string l0, string l1)

myConn = new conn();
string L0Sql = "select L0,material from inv_master where L1='0' and L2='0' and isvisible='Y' order by L0";
SqlDataAdapter L0Da = new SqlDataAdapter(L0Sql, myConn.con);
DataSet L0Ds = new DataSet();
L0Da.Fill(L0Ds);
for (int i = 0; i < L0Ds.Tables[0].Rows.Count; i++)

DataRowView L0drv = L0Ds.Tables[0].DefaultView[i];
TreeNode childNode = new TreeNode();
childNode.Text = L0drv[1].ToString().Trim();
childNode.Value = L0drv[0].ToString().Trim();
childNode.Expanded = (L0drv[0].ToString().Trim() == l0) ? true : false;
childNode.SelectAction = TreeNodeSelectAction.Expand;
TreeView1.Nodes.Add(childNode);
string L1Sql = "select L1,material,inv_code from inv_master where L0='" + L0drv[0].ToString().Trim() + "' and L2='0' and L1<>'0' and isvisible='Y' order by L1";
SqlDataAdapter L1Da = new SqlDataAdapter(L1Sql, myConn.con);
DataSet L1Ds = new DataSet();
L1Da.Fill(L1Ds);
TreeNode parentNode2 = childNode;
for (int j = 0; j < L1Ds.Tables[0].Rows.Count; j++)

DataRowView L1drv = L1Ds.Tables[0].DefaultView[j];
TreeNode childNode2 = new TreeNode();
childNode2.Text = L1drv[1].ToString().Trim();
childNode2.Value = L1drv[0].ToString().Trim();
childNode2.Expanded = (L1drv[0].ToString().Trim() == l1) ? true : false;
childNode2.SelectAction = TreeNodeSelectAction.Expand;
parentNode2.ChildNodes.Add(childNode2);
string L2Sql = "select L2,material,inv_code from inv_master where L0='" + L0drv[0].ToString().Trim() + "' and L1='" + L1drv[0].ToString().Trim() + "' and L2<>'0' and isvisible='Y' order by L2";
SqlDataAdapter L2Da = new SqlDataAdapter(L2Sql, myConn.con);
DataSet L2Ds = new DataSet();
L2Da.Fill(L2Ds);
TreeNode parentNode3 = childNode2;
for (int k = 0; k < L2Ds.Tables[0].Rows.Count; k++)

DataRowView L2drv = L2Ds.Tables[0].DefaultView[k];
TreeNode childNode3 = new TreeNode();
childNode3.Text = L2drv[1].ToString().Trim();
childNode3.Value = L2drv[2].ToString().Trim();
parentNode3.ChildNodes.Add(childNode3);



myConn.con.Close();

protected void TreeView1_SelectedNodeChanged(object sender, EventArgs e)//当物料改变时

Label1.Text = "物料名称:" + TreeView1.SelectedNode.Text;
int inv_code = Convert.ToInt32(TreeView1.SelectedNode.Value);

以上是关于急 asp.net TreeView 动态数据绑定的主要内容,如果未能解决你的问题,请参考以下文章

asp.net 2.0中TREEVIEW中动态增加结点

ASP.NET 递归将分类绑定到 TreeView

使用treeview控件读数据库动态生成带checkbox复选框的树形导航菜单asp.net(C#),急求帮助还可加分

vs2010中GridView控件怎么用?急!!!

动态生成树,用的是VS自带TreeView控件。我想做个右键弹出的菜单来添加删除。做的是web页面。应该怎么做。

WPF中TreeView控件数据绑定和后台动态添加数据