我需要扩展TreeNode类,以便我可以为每个节点添加自定义属性(看作WebForms TreeNode不包含Tag属性).所以这是我的CustomTreeNode:
public class CustomTreeNode : TreeNode
{
public CustomTreeNode()
{
}
public CustomTreeNode(int nodeId, string nodeType)
{
NodeId = nodeId;
NodeType = nodeType;
}
public string NodeType { get; set; }
public int NodeId { get; set; }
}
如果我创建一个CustomTreeNode并将其添加到TreeView:
CustomTreeNode node = new CustomTreeNode(1, "CustomType");
treeView.Nodes.Add(node);
然后我会得到一个执行以下的转换异常:
CustomTreeNode selectedNode = (CustomTreeNode)TreeView.SelectedNode;
因为TreeView返回TreeNode,而不是CustomTreeNode.
我做了一些阅读,看起来我需要扩展TreeView类,并覆盖CreateNode()方法以返回CustomTreeNode而不是TreeNode.所以我创造了这个:
public class CustomTreeView : TreeView
{
protected override TreeNode CreateNode()
{
return new CustomTreeNode();
}
}
但问题是,CreateNode()不接受任何参数,因此您必须调用CustomTreeNode类的空构造函数.因此,当我在上面创建CustomTreeNode时,当我从CustomTreeView返回时,nodeId和nodeType值已经丢失,因为空构造函数返回没有任何值的节点.
任何帮助非常感谢.
解决方法:
这就是我提出的(专家,欢迎任何建议).在后面的代码中实例化CustomTreeNodes并通过setter设置属性.修改CustomTreeNode类以在ViewState中保留值.自定义树视图的CreateNode返回的节点将加载ViewState信息.
TreeNode类:
[DefaultProperty("Text")]
[ToolboxData("<{0}:CustomTreeNode runat=server></{0}:CustomTreeNode>")]
public class CustomTreeNode : TreeNode
{
private const int NODE_TYPE = 1;
private const int NODE_ID = 2;
public string NodeType { get; set; }
public int NodeId { get; set; }
protected override void LoadViewState(Object savedState)
{
if (savedState != null)
{
object[] myState = (object[])savedState;
if (myState[0] != null)
base.LoadViewState(myState[0]);
if (myState[NODE_TYPE] != null)
this.NodeType = (string)myState[NODE_TYPE];
if (myState[NODE_ID] != null)
this.NodeId = (int)myState[NODE_ID];
}
}
protected override Object SaveViewState()
{
object baseState = base.SaveViewState();
object[] allStates = new object[3];
allStates[0] = baseState;
allStates[NODE_TYPE] = this.NodeType;
allStates[NODE_ID] = this.NodeId;
return allStates;
}
}
TreeView类:
[DefaultProperty("Text")]
[ToolboxData("<{0}:CustomTreeView runat=server></{0}:CustomTreeView>")]
public class CustomTreeView : TreeView
{
protected override TreeNode CreateNode()
{
// Tree node will get its members populated with the data from VIEWSTATE
return new CustomTreeNode();
}
}
简单的.aspx文件(假设您的自定义控件在程序集“Foo”和名称空间“Bar”中定义:
<%@ Register TagPrefix="customControl" Assembly="Foo" Namespace="Bar" %>
<customControl:CustomTreeView ID="sampleTree"
runat="server" onselectednodechanged="sampleTree_SelectedNodeChanged"></customControl:CustomTreeView>
<asp:Label ID="lblSelectedNode" runat="server" ></asp:Label>
代码隐藏:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
PopulateTree();
}
}
private void PopulateTree()
{
sampleTree.Nodes.Clear();
CustomTreeNode root = new CustomTreeNode();
root.Value = "root node";
sampleTree.Nodes.Add(root);
// Creating some fake nodes (you would of course be using real data)
for (int i = 0; i < 10; i++)
{
CustomTreeNode child = new CustomTreeNode();
child.NodeId = i; // Saved in ViewState
child.NodeType = "Type " + i; // Saved in ViewState
child.Value = child.NodeType;
root.ChildNodes.Add(child);
}
}
protected void sampleTree_SelectedNodeChanged(object sender, EventArgs e)
{
CustomTreeView cTreeView = (CustomTreeView) sender;
lblSelectedNode.Text = ((CustomTreeNode)cTreeView.SelectedNode).NodeType;
}