(100分)'TreeView1_data'未定义的问题 在线等 急!急!
用TreeView动态从数据库里面读取数据,有时会出现'TreeView1_data'未定义的错误,我在网络上查了一下,主要是由于该控件没有加载完毕所引起的,
我想请问有什么好的解决办法?
或是如何判断这个控件是否加载完毕,代码应该怎么写?
谢谢大家!
如果分不够可以再加! --------------------编程问答-------------------- http://topic.csdn.net/u/20071104/11/f087b899-7750-4087-8030-343a7885623c.html
有个兄弟遇到了与我同样的问题! --------------------编程问答-------------------- 请大家帮忙啊,着急! --------------------编程问答-------------------- 代码贴出来看看 --------------------编程问答-------------------- 具体一点,贴点关键代码看看
protected void BindTree(TreeNode tn, DataRow[] rows)
{
TreeNode node;
if(rows.count < 1)
{return;}
foreach (DataRow row in rows)
{
node = new TreeNode();
node.Text = row[ "menu_name "].ToString();
node.NavigateUrl = row[ "URL "].ToString() + "?ID= " + row[ "menu_id "].ToString();
node.Target = "rightFrame ";
tn.ChildNodes.Add(node);
DataRow[] ChildRows = row.GetChildRows( "relation ");
if (ChildRows.Length != 0)
{
BindTree(node, ChildRows);
}
}
}
protected void BindTree()
{
Users U = new Users();
U.Name = Session[ "LoginUserName "].ToString();
DataSet ds = U.GetMenusByName();
if(ds==null ||ds.tables.count<1)
{return;}
DataTable dt = ds.Tables[0];
DataRelation dr = new DataRelation( "relation ", dt.Columns[ "menu_id "], dt.Columns[ "father_id "]);
dt.DataSet.Relations.Add(dr);
DataRow[] rows = dt.Select( "father_id is null ");
if(rows.count < 1)
{return;}
TreeNode node;
foreach (DataRow row in rows)
{
node = new TreeNode();
node.Text = row[ "menu_name "].ToString();
node.NavigateUrl = row[ "URL "].ToString() + "?ID= " + row[ "menu_id "].ToString();
node.Target = "rightFrame ";
this.TreeView1.Nodes.Add(node);
DataRow[] ChildRows = row.GetChildRows( "relation ");
if (ChildRows.Length != 0)
{
BindTree(node, ChildRows);
}
}
}
--------------------编程问答--------------------
--------------------编程问答-------------------- 自己UP一下!!! --------------------编程问答-------------------- 自己UP一下!!! --------------------编程问答-------------------- TreeView1.Nodes.Add(tn);
//开始动态绑定导航栏
DataView DV3 = Socut.Data.ExecuteDataSet("select title,id from vod_catalog order by number desc").Tables[0].DefaultView;
foreach (DataRowView datav in DV3)
{
TreeNode tn = new TreeNode();
tn.Text = datav["title"].ToString();
tn.Value = datav["id"].ToString();
tn.Expanded = false;
tn.SelectAction = TreeNodeSelectAction.Expand;
TreeView1.Nodes.Add(tn);
AddChildNodes(tn);
}
public void AddChildNodes(TreeNode tnChild)
{
int bid = Convert.ToInt32(tnChild.Value);
DataView DV = Socut.Data.ExecuteDataSet("select title,id from vod where catalog_id=" + bid + "").Tables[0].DefaultView;
foreach (DataRowView datav in DV)
{
TreeNode tn2 = new TreeNode();
tn2.Text = datav["title"].ToString();
tn2.Value = datav["id"].ToString();
tn2.NavigateUrl = "vod.aspx?id=" + datav["id"].ToString();
tnChild.ChildNodes.Add(tn2);
}
}
AddChildNodes(tn);
换一下呢,先生成字节点,再添到树上 --------------------编程问答-------------------- 还是不行,如果刷新过快,还是一样会出现'TreeView1_data'未定义的问题的错误! --------------------编程问答-------------------- 等了几天了,有没有人帮个忙吗?难道CSDN不是从前的CSDN! --------------------编程问答-------------------- 关注 --------------------编程问答-------------------- 再次UP --------------------编程问答-------------------- 脚本库出错了,还是没放好位转置
修改treeview 所需的脚本文件的路径 --------------------编程问答--------------------
--------------------编程问答-------------------- 帮顶一下 都好几天了 好可怜 --------------------编程问答-------------------- 哎,没人啊!!! --------------------编程问答-------------------- protected void Page_Load(object sender, EventArgs e)
// 循环结果集
private void CreateTree(TreeNodeCollection Nds, string sParentId)
{
DataView dv = new DataView();
TreeNode tmpNd;
dv.Table = oDs.Tables[0];
dv.RowFilter = "parent_code='" + sParentId + "'";
foreach (DataRowView drv in dv)
{
tmpNd = new TreeNode();
tmpNd.Value = drv["menu_code"].ToString();
tmpNd.Text = drv["menu_name"].ToString();
Nds.Add(tmpNd);
CreateTree(tmpNd.ChildNodes, drv["menu_code"].ToString());
}
}
{
if (Session["EmpName"] == null)
{
Response.Write("<script>top.location.href='index.aspx';</script>");
return;
}
if (!IsPostBack)
{
//取得所有节点并返回DS
string empID = treeMenu.selectEmpInfoByEmpName(Session["EmpName"].ToString()).Tables[0].Rows[0]["EmpID"].ToString();
DataSet ds = treeMenu.SelectMobuldInfo(Int32.Parse(empID));
DataTable dt = new DataTable();
dt = ds.Tables[0];
AddNodes(dt, null, "0");
}
}
//递归添加节点
private void AddNodes(DataTable dt, TreeNode node, string id)
{
DataRow[] rows = dt.Select("ParentModuleID=" + id); //筛选出属于父节点“node”的子节点集合
foreach (DataRow dr in rows) //循环子节点集合
{
TreeNode nd = new TreeNode();
nd.Value = dr["quanXianID"].ToString(); //存放节点ID
nd.Text = dr["quanXianMingCheng"].ToString(); //设置节点名称
nd.ImageUrl = dr["imageURL"].ToString();//设置节点的图片
nd.NavigateUrl = dr["URl"].ToString();//设置节点的链接
nd.Target = "MainFrame";
this.AddNodes(dt, nd, nd.Value); //递归,添加该节点的子节点
if (node == null || id == "0")
{
TreeView1.Nodes.Add(nd); //添加至根节点
}
else
{
node.ChildNodes.Add(nd);//添加子节点
}
}
} --------------------编程问答-------------------- <code>
<div id="divTreeView" style="display:none;">
<asp:TreeView
ID="TreeView1" ...
</asp:TreeView>
</div>
</code>
然后,在显示控件的页面加入:
<code>
<head>
<script language="javascript">
function showTree() {
var elTree = document.getElementById('divTreeView');
elTree.style.display = 'block';
}
</script>
</head>
<body onload="showTree();" >
......
至此问题搞定,再不会出现随机报错的的提示了,唯一不足的是加载有些延迟,不会不明显,可以忍受. --------------------编程问答-------------------- 在设计视图的属性窗口中将TreeView的属性EnableClientScript设置为false即可 --------------------编程问答-------------------- private static DataView dv;
protected void Page_Load(sender,e)
{
if(!IsPostBack)
binderTreeView(null,"0");
}
private void binderTreeView(TreeNode treeNode,string ParentIndex)
{
if(dv==null)
{
using(SqlConnection conn=new SqlConnection())
{
string connString="server=.;uid=sa;pwd=;DataBase=db";
conn.ConnectionStrng = connString;
SqlDataAdapter sda=new SqlDataAdapter("select * from Model",conn);
DataSet ds=new DataSet();
sda.fill(ds);
dv=ds.tables[0].defaultView;
}
dv.RowFilter="ParentIndex=" + ParentIndex;
if(dv.count>0)
{
foreach(DataRowView drv in dv)
{
TreeNode node=new TreeNode();
node.Text=drv["Text"].ToString();
node.Value=drv["Value"].ToString();
node.NavigateUrl = drv["URl"].ToString();
if(treeNode==null)
this.TreeView1.Nodes.Add(node);
else
treeNode.ChildNodes.Add(node);
binderTreeView(node,node.Value);
}
}
}
} --------------------编程问答-------------------- 学习了 --------------------编程问答-------------------- 没遇到过 --------------------编程问答--------------------
string sql = "select * from parts where parentid=" + pid;
DataSet ds = db.ExecuteDataSet(sql, null, CommandType.Text);
// DataSet ds = bll.GetparentID(pid);
if (ds != null && ds.Tables.Count > 0)
{
foreach (DataRow dr in ds.Tables[0].Rows)
{
TreeNode chilenode = new TreeNode();
chilenode.Text = dr["partname"].ToString();
chilenode.NavigateUrl = "NotEndCard.aspx?uid=" + dr["partid"].ToString();
chilenode.Target = "right";
//给当前节点添加子节点
AddChild(chilenode, int.Parse(dr["partid"].ToString()));
if (node == null)
{
TreeView1.Nodes.Add(chilenode);
}
else
{
node.ChildNodes.Add(chilenode);
}
}
}
补充:.NET技术 , ASP.NET