TreeView 不显示来自 Web 窗体项目中对 OnSelectedNodeChange 的函数调用的已检查节点
TreeView not showing checked nodes from function call to OnSelectedNodeChange in a Web Forms projet
我只想显示选中节点的值,并且我有以下相关代码
Default.aspx
<asp:Literal ID="Message" runat="server"></asp:Literal>
<asp:Button ID="Button1" runat="server" Text="Button" Width="56px"
OnClick="Select_Change" />
<div>
<asp:TreeView ID="TreeView1" runat="server"
ShowCheckBoxes="All"
OnSelectedNodeChanged="Select_Change"
>
</asp:TreeView>
</div>
Default.aspx.cs
protected void Page_Load(object sender, EventArgs e)
{
//These variables and methods create a hierarchy of the folder structure.
var latestDirectory = DirectoryList().First().FullName.ToString();
this.directoryFiles = DirSearch(latestDirectory);
this.directoryArray = dirArray;
this.directoryString.Text = latestDirectory;
ListDirectory(TreeView1, latestDirectory);
}
protected void Select_Change(object sender, EventArgs e)
{
string message = "selected nodes \n";
foreach(TreeNode item in TreeView1.CheckedNodes)
{
message += item.Text;
}
Message.Text = message;
}
当我单击该按钮时,它只显示通用的“选定节点”,后面什么也没有,即使在选定节点之后也是如此。关于我在这里遗漏的任何想法?
选中的项目,只是一个项目,你要勾选值的集合。
这个:
For Each tN As TreeNode In TreeView1.CheckedNodes
Debug.Print("Sel node = " & tN.Value & "," & tN.Text)
Next
或
foreach (TreeNode tN in TreeView1.CheckedNodes)
{
Debug.Print("Sel node = " & tN.Value & "," & tN.Text);
message += tN.Value.ToString() + "->" + tN.Text + "| ";
}
那么,您的代码是否正在同时设置值和文本?或者您可能只是在设置 .Value。不清楚电视是如何加载的?
编辑 ======================================= ================
让我们举一个完整的例子。我们有这个:
<asp:TreeView ID="TreeView1" runat="server"
ShowCheckBoxes="All">
</asp:TreeView>
<br />
<asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />
<br />
<asp:TextBox ID="TextBox1" runat="server" Width="347px"></asp:TextBox>
代码:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
LoadTree();
}
void LoadTree()
{
for (int i = 1; i <= 5; i++)
TreeView1.Nodes.Add(new TreeNode("Tx " + i, "Vx" + i));
}
protected void Button1_Click(object sender, EventArgs e)
{
string sChoices = "";
foreach (TreeNode tN in TreeView1.CheckedNodes)
sChoices += "(" + tN.Value + "," + tN.Text + ") ";
TextBox1.Text = sChoices;
}
所以,我们 运行 并检查前两个,这样说:
我只想显示选中节点的值,并且我有以下相关代码
Default.aspx
<asp:Literal ID="Message" runat="server"></asp:Literal>
<asp:Button ID="Button1" runat="server" Text="Button" Width="56px"
OnClick="Select_Change" />
<div>
<asp:TreeView ID="TreeView1" runat="server"
ShowCheckBoxes="All"
OnSelectedNodeChanged="Select_Change"
>
</asp:TreeView>
</div>
Default.aspx.cs
protected void Page_Load(object sender, EventArgs e)
{
//These variables and methods create a hierarchy of the folder structure.
var latestDirectory = DirectoryList().First().FullName.ToString();
this.directoryFiles = DirSearch(latestDirectory);
this.directoryArray = dirArray;
this.directoryString.Text = latestDirectory;
ListDirectory(TreeView1, latestDirectory);
}
protected void Select_Change(object sender, EventArgs e)
{
string message = "selected nodes \n";
foreach(TreeNode item in TreeView1.CheckedNodes)
{
message += item.Text;
}
Message.Text = message;
}
当我单击该按钮时,它只显示通用的“选定节点”,后面什么也没有,即使在选定节点之后也是如此。关于我在这里遗漏的任何想法?
选中的项目,只是一个项目,你要勾选值的集合。
这个:
For Each tN As TreeNode In TreeView1.CheckedNodes
Debug.Print("Sel node = " & tN.Value & "," & tN.Text)
Next
或
foreach (TreeNode tN in TreeView1.CheckedNodes)
{
Debug.Print("Sel node = " & tN.Value & "," & tN.Text);
message += tN.Value.ToString() + "->" + tN.Text + "| ";
}
那么,您的代码是否正在同时设置值和文本?或者您可能只是在设置 .Value。不清楚电视是如何加载的?
编辑 ======================================= ================ 让我们举一个完整的例子。我们有这个:
<asp:TreeView ID="TreeView1" runat="server"
ShowCheckBoxes="All">
</asp:TreeView>
<br />
<asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />
<br />
<asp:TextBox ID="TextBox1" runat="server" Width="347px"></asp:TextBox>
代码:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
LoadTree();
}
void LoadTree()
{
for (int i = 1; i <= 5; i++)
TreeView1.Nodes.Add(new TreeNode("Tx " + i, "Vx" + i));
}
protected void Button1_Click(object sender, EventArgs e)
{
string sChoices = "";
foreach (TreeNode tN in TreeView1.CheckedNodes)
sChoices += "(" + tN.Value + "," + tN.Text + ") ";
TextBox1.Text = sChoices;
}
所以,我们 运行 并检查前两个,这样说: