我在内容页面中使用了treeview。 Treeview正确填充。 (即子节点和父节点)我在获取树视图的CheckedNodes.Count属性时遇到问题。 下面是我的asp.net treeview标签:
<asp:TreeView ID="TreeView1" runat="server" ShowCheckBoxes="Leaf" class="ztree">
<ParentNodeStyle Font-Bold="True" />
<SelectedNodeStyle BackColor="#B5B5B5" Font-Underline="False" HorizontalPadding="0px"
VerticalPadding="0px" />
</asp:TreeView>
将日期绑定到TreeView的代码
private void PopulateTreeView(DataTable dtParent, int parentId, TreeNode treeNode)
{
foreach (DataRow masterRow in dtParent.Rows)
{
TreeNode masterNodes = new TreeNode
{
Text = masterRow["questionText"].ToString(),
Value = masterRow["questionId"].ToString()
};
if (parentId == 0)
{
TreeView1.Nodes.Add(masterNodes);
DataTable dtChild = this.GetData("SELECT choiceIndex, choiceName FROM dbo.VWDummy WHERE questionId = " + masterNodes.Value);
PopulateChildTreeView(dtChild, int.Parse(masterNodes.Value), masterNodes);
}
}
}
private void PopulateChildTreeView(DataTable dtChild, int parentId, TreeNode treeNode)
{
foreach (DataRow row in dtChild.Rows)
{
TreeNode child = new TreeNode
{
Text = row["choiceName"].ToString(),
Value = row["choiceIndex"].ToString()
};
treeNode.ChildNodes.Add(child);
}
}
在页面后面点击以下按钮代码。
<asp:LinkButton ID="LinkButton1" runat="server" OnClick="test">Done</asp:LinkButton>
protected void test(object sender, EventArgs e)
{
string childValue;
string childtext;
string parentval;
int counter = 0;
if (TreeView1.CheckedNodes.Count > 0)
{
foreach (TreeNode node in TreeView1.CheckedNodes)
{
if (node.Parent != null)
{
parentval = node.Parent.Value.ToString();
childtext = node.Text.ToString();
childValue = node.Value.ToString();
questId.Add(parentval);
choiceIndex.Add(childValue);
counter++;
}
在调试时,我发现“TreeView1.CheckedNodes.Count”返回0计数,即使我选择了3个子节点。
我该如何解决这个问题?