I just want to show checked node's values and I've got the following code regarding that
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;
}
When I click the button, it just shows the generic "selected nodes" with nothing after it, even after selected node(s). Any ideas as to what I'm missing here?
CodePudding user response:
The selected item, is just a item, you want the collection of checked values.
this:
For Each tN As TreeNode In TreeView1.CheckedNodes
Debug.Print("Sel node = " & tN.Value & "," & tN.Text)
Next
or
foreach (TreeNode tN in TreeView1.CheckedNodes)
{
Debug.Print("Sel node = " & tN.Value & "," & tN.Text);
message = tN.Value.ToString() "->" tN.Text "| ";
}
So, is your code beind setting both a value and text? Or perhaps you are just setting the .Value. It not clear how the TV is being loaded up?
EDIT ======================================================= Lets take an full example. We have this:
<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>
Code:
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;
}
So, we run and check the first two, say like this: