How do I capture the event of the clicking the Selected Node of a TreeView? It doesn\'t fire the SelectedNodeChanged since the selection has obviously not c
c#:
TreeNode node = TreeTypes.FindNode(obj.CustomerTypeId.ToString());
TreeTypes.Nodes[TreeTypes.Nodes.IndexOf(node)].Select();
Store what is selected and use code in the Page_Load event handler to compare what is selected to what you have stored. Page_Load is called for every post back even if the selected value doesn't change, unlike SelectedNodeChanged.
Example
alt text http://smithmier.com/TreeViewExample.png
html
<form id="form1" runat="server">
<div>
<asp:TreeView ID="TreeView1" runat="server" OnSelectedNodeChanged="TreeView1_SelectedNodeChanged"
ShowLines="True">
<Nodes>
<asp:TreeNode Text="Root" Value="Root">
<asp:TreeNode Text="RootSub1" Value="RootSub1"></asp:TreeNode>
<asp:TreeNode Text="RootSub2" Value="RootSub2"></asp:TreeNode>
</asp:TreeNode>
<asp:TreeNode Text="Root2" Value="Root2">
<asp:TreeNode Text="Root2Sub1" Value="Root2Sub1">
<asp:TreeNode Text="Root2Sub1Sub1" Value="Root2Sub1Sub1"></asp:TreeNode>
</asp:TreeNode>
<asp:TreeNode Text="Root2Sub2" Value="Root2Sub2"></asp:TreeNode>
</asp:TreeNode>
</Nodes>
</asp:TreeView>
<asp:Label ID="Label1" runat="server" Text="Selected"></asp:Label>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:Label ID="Label2" runat="server" Text="Label"></asp:Label></div>
</form>
C#
protected void Page_Load(object sender, EventArgs e)
{
if(TreeView1.SelectedNode!=null && this.TextBox1.Text == TreeView1.SelectedNode.Value.ToString())
{
Label2.Text = (int.Parse(Label2.Text) + 1).ToString();
}
else
{
Label2.Text = "0";
}
}
protected void TreeView1_SelectedNodeChanged(object sender, EventArgs e)
{
this.TextBox1.Text = TreeView1.SelectedNode.Value.ToString();
}
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
TreeView1.SelectedNode.Selected = false;
}
}
works for me