Click here to Skip to main content
15,909,242 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am having a tree view control and i doing some operation in the selected node changed event.
The event is not firing when i click in the node at runtime. This is the code i am using.
The break point is not getting hit at runtime.

C#
protected void tvData_SelectedNodeChanged(object sender, EventArgs e)
        {
            if (tvData.SelectedNode.Text == "Country")
            {
                tdCtry.Visible = false;
            }
            else
            {
                tdCtry.Visible = true;
            }
        }

Can anyone tell me what needs to be done?
Posted
Comments
norbitrial 13-Jun-14 7:54am    
Can you post the aspx code for your treeview? Have you set for treeview runat="server"?
Member 10755410 13-Jun-14 8:19am    
<asp:TreeView ID="tvData" runat="server" Target="frm" Height="100%"
SkipLinkText="" Font-Bold="True" BorderColor="Black" ShowLines="True"
onselectednodechanged="tvData_SelectedNodeChanged" Width="240px">
<nodestyle font-size="Small">
<SelectedNodeStyle ForeColor="Red" />
<hovernodestyle forecolor="Red">
<leafnodestyle forecolor="Black" font-size="10pt">
<nodes>
<asp:TreeNode Text="Data" Value="Data">
<asp:TreeNode Text="Category" Value="Category">
<asp:TreeNode NavigateUrl="~/Country.aspx" Target="frm" Text="Country" Value="Country">

<asp:TreeNode Text="Sub Category" Value="Sub Category">
<asp:TreeNode NavigateUrl="~/City.aspx" Target="frm" Text="City"
Value="City">




1 solution

There are some articles what you can find around this topic. You need to force postback to your TreeView.

You need to create a forcing JavaScript function first:
JavaScript
function forcePostBack() {
    var yourObject = window.event.srcElement;
    if (yourObject .tagName == "INPUT" && yourObject.type == "checkbox") {
           __doPostBack("","");
    } 
}


After in page load method you need to add click event for your TreeView
C#
protected void Page_Load(object sender, EventArgs e)
{
    tvData.Attributes.Add("onclick", "forcePostBack()");
}


After you need to use TreeNodeCheckChanged event instead of SelectedNodeChanged as follow:
C#
protected void tvData_TreeNodeCheckChanged(object sender, TreeNodeEventArgs e)
{
    //in this way easier to set visibility
    tdCtry.Visible = !tvData.SelectedNode.Text.Equals("Country");
}


Please let me know if it's working or not!
 
Share this answer
 
v2
Comments
Member 10755410 13-Jun-14 8:40am    
what does that forcePostBack() function do?
goathik 13-Jun-14 9:02am    
it literally forces a post back on your page...
goathik 13-Jun-14 9:10am    
Try his solution, from the search i have made, unfortunently it seems that that node selection event is a client side one.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900