注册 登录
编程论坛 ASP.NET技术论坛

[求助]从TreeView中读取id时出错

hinroe 发布于 2007-07-31 22:22, 570 次点击

if (TreeViewCategory.Nodes[0].ChildNodes.Count > 0)//取出cid的值
{
if (Session["CategoryID"] != null)
{
ViewState["cid"] = int.Parse(Session["CategoryID"].ToString());
cid=int.Parse(ViewState["cid"].ToString());
}
else
{
Session["CategoryID"] = TreeViewCategory.Nodes[0].ChildNodes[0].Value;
ViewState["cid"] = TreeViewCategory.Nodes[0].ChildNodes[0].Value;
cid = int.Parse(ViewState["cid"].ToString());
}
}
TreeViewCategory.Attributes.Add("onclick", "doSelect(this)");
iframeEdit.Attributes.Add("src", "AttachmentTypeList.aspx?id="+cid);

为什么我都没有改动这个页,突然之间运行程序就提示我“使用了未赋值的局部变量“cid”,为什么提取不了cid的值,是怎么回事,请求支援

4 回复
#2
川流不息2007-07-31 23:49
你要看一下那些判断条件是否有不对的地方。
这一个地方:if (TreeViewCategory.Nodes[0].ChildNodes.Count > 0)//取出cid的值
这一个地方:if (Session["CategoryID"] != null)
#3
川流不息2007-07-31 23:49
你设断点跑一下看看。
#4
hinroe2007-08-01 08:34
我设断点了,它跑都不跑,从进去Page_Load开始int cid;的定义就没走,直接提示我以上错误,再帮看看吧!第一步就不对,下面的都不知道该怎么办了,以前这样写都对,突然不知道改哪了就出现这个问题了
#5
hinroe2007-08-01 09:32

问题解决了,因为我在添加树的方法中已经将节点的值作为id值传过去了

public void AddTree(int ParentID, TreeNode pNode, DataTable dt)
{
DataView dvTree = new DataView(dt);
dvTree.RowFilter = "[ParentID] = " + ParentID;

foreach (DataRowView Row in dvTree)
{
TreeNode node = new TreeNode();
count++;

node.Text = Row["Name"].ToString();

node.Value = Row["CategoryID"].ToString();
if (Row["Level"].ToString() != "1")
{
node.NavigateUrl = "javascript:openCourseware(" + node.Value + ",this);";//这里
}
else
{
node.NavigateUrl = "javascript:openCourseware(" + node.Value + ",this);";
}
if (Session["CategoryID"] != null && Session["CategoryID"].ToString() == node.Value)//对Session进行判断
{
Session.Remove("CategoryID");
node.Selected = true;
node.Expand();
iframeEdit.Attributes.Add("src", "AttachmentTypeList.aspx?id=" + node.Value);
pNode.ChildNodes.Add(node);
node.Parent.Expand();
if (node.Parent.Parent != null) node.Parent.Parent.Expand();
}
else
{
if (count == 1)

{
node.Selected = true;
iframeEdit.Attributes.Add("src", "AttachmentTypeList.aspx?id=" + node.Value);
}
pNode.ChildNodes.Add(node);
}
AddTree((int)Row["CategoryID"], node, dt);

}
}

然后在.aspx页中写脚本

<script>
function openCourseware(id,node)
{
if (id>0)
{
document.all.iframeEdit.src="AttachmentTypeList.aspx?id="+id;
}

node.Selected=true;
}
</script>

就OK了

1