I have a TreeView control in my ASP.NET web application, and I have problems with the event selected node changed, I click on some node in the treeview but the event doesn’t trigger, I have some instructions there that are not executed, I checked with debugger also.
<asp:TreeView ID="ArboreSarcini" runat="server" ImageSet="News"
onselectednodechanged="ArboreSarcini_SelectedNodeChanged" NodeIndent="10"
style="z-index: 1; left: 1px; top: 27px; position: absolute; height: 308px; width: 446px">
<HoverNodeStyle Font-Underline="True" BackColor="#99CCCC" Font-Size="12pt" />
<LeafNodeStyle ImageUrl="~/Poze/leaf.png" NodeSpacing="2px" />
<LevelStyles>
<asp:TreeNodeStyle BorderColor="Black" BorderStyle="Solid" BorderWidth="1px"
Font-Names="Leelawadee" Font-Size="Medium" Font-Underline="False"
ImageUrl="~/Poze/Root.png" />
<asp:TreeNodeStyle BorderColor="#003300" BorderStyle="Solid" BorderWidth="1px"
Font-Names="Leelawadee" Font-Underline="False" ImageUrl="~/Poze/node.png"
Width="400px" />
<asp:TreeNodeStyle BorderColor="#006600" BorderStyle="Solid" BorderWidth="1px"
Font-Names="Leelawadee" Font-Underline="False" Height="10px"
ImageUrl="~/Poze/leaf.png" Width="390px" />
</LevelStyles>
<NodeStyle Font-Names="Leelawadee" Font-Size="10pt" ForeColor="Black"
HorizontalPadding="5px" NodeSpacing="2px" VerticalPadding="0px"
ImageUrl="~/Poze/node.png" Width="0px" />
<ParentNodeStyle Font-Bold="False" Width="500px" />
<RootNodeStyle ImageUrl="~/Poze/Root.png" />
<SelectedNodeStyle Font-Underline="False" BackColor="#009148" BorderWidth="2px"
Font-Bold="False" Font-Italic="True" Font-Size="12pt" />
</asp:TreeView>
protected void Button1_Click(object sender, EventArgs e)
{
ArboreSarcini.Nodes.Clear();
populeaza_sarcini();
string sqlstring1 = "Select * from activitati";
System.Data.SqlClient.SqlConnection con1 = new System.Data.SqlClient.SqlConnection(
"Data Source=BOGDAN-PC\\BOGDAN;Initial Catalog=ePlanning;Integrated Security=SSPI;Connect Timeout=10;TrustServerCertificate=True ");
System.Data.SqlClient.SqlCommand comm1 = new System.Data.SqlClient.SqlCommand(sqlstring1, con1);
System.Data.SqlClient.SqlDataReader reader1;
con1.Open();
reader1 = comm1.ExecuteReader();
while (reader1.Read())
{
foreach (Sarcina s in listaSarcini)
{
if ((int)reader1["id_sarcina"] == s.Id_sarcina)
{
s.ListaActivitati.Add(new Activitate(Convert.ToInt32(reader1["id_activitate"]), reader1["descriere"].ToString()));
}
}
}
TreeNode tatal = new TreeNode();
tatal.Value = DropListProiecte.SelectedItem.ToString();
// ArboreSarcini.Nodes.Add(tatal);
TreeNode parentNode = new TreeNode();
foreach (Sarcina sarc in listaSarcini)
{
parentNode = new TreeNode( sarc.Id_sarcina.ToString() + ". " + sarc.Descriere.ToString());
foreach (Activitate act in sarc.ListaActivitati)
{
TreeNode copil = new TreeNode(act.Id_activitate.ToString()+". "+act.Descriere.ToString() );
parentNode.ChildNodes.Add(copil);
}
tatal.ChildNodes.Add(parentNode);
//parentNode.Collapse();
}
ArboreSarcini.Nodes.Add(tatal);
con.Close();
ArboreSarcini.ExpandAll();
}
protected void ArboreSarcini_SelectedNodeChanged(object sender, EventArgs e)
{
if (ArboreSarcini.SelectedNode.ImageUrl == "~/Poze/node.png")
{
System.Data.SqlClient.SqlConnection con = new System.Data.SqlClient.SqlConnection(
"Data Source=BOGDAN-PC\\BOGDAN;Initial Catalog=ePlanning;Integrated Security=SSPI;Connect Timeout=10;TrustServerCertificate=True ");
// string de_splituit = ArboreSarcini.SelectedNode.Text;
string[] id_sarcina = ArboreSarcini.SelectedNode.Text.Split('.');
string sqlstring = "Select * from sarcini where id_sarcina=" + id_sarcina[0] + ";";
System.Data.SqlClient.SqlCommand comm = new System.Data.SqlClient.SqlCommand(sqlstring, con);
System.Data.SqlClient.SqlDataReader reader;
con.Open();
reader = comm.ExecuteReader();
while (reader.Read())
{
tbIDSarcina.Text = reader["id_sarcina"].ToString();
}
}
}
So kindly verify that you set all the required properties. Sharing your HTML might helpful.
Read more about the asp.net treeview here
TreeNode.Nodes Property.
Gets the collection of TreeNode objects assigned to the current tree node.
http://msdn.microsoft.com/en-us/library/system.windows.forms.treenode.nodes.aspx