C# – ASP.NET PostBack on selecting checkbox of treeview

asp.netcpostbacktreeview

I have an asp.net project and working in C#.

In my project I have a databound listbox that has checkboxes.

When the user clicks on a checkbox it should for an example update a label/textbox.

The thing is, it doesnt update the label/textbox until I click on a button that does a postback. How will I Call a postback on the checkbox changed event, since the "OnTreeNodeCheckChanged" event looks like it only fires once the postback has been triggered?
Is this even a good idea (to want to call a postback every time the a checkbox has been changed)

–Updated code Snippet–
Asp

 <asp:TreeView ID="treevCourses" runat="server" AutoPostBack="true" ShowCheckBoxes="All" Width="100%"
                OnTreeNodeCheckChanged="check_changed" Height="16px" ImageSet="Contacts">

(Tried having the handler in the C# part.)
C#

protected void check_changed(object sender, TreeNodeEventArgs e)
        {
        lblTest.Text = "TestText";
        }

(Also tried having it in the script part)

void check_changed(object sender, EventArgs e)
    {
        lblTest.Text = "TestText";
    }

Binding data to the Treeview (this happens on a button postback)

foreach (DataRow row in ds.Tables[0].Rows)
                {
                    TreeNode node = new TreeNode(row["courseName"].ToString(), row["courseName"].ToString());
                    //  node.PopulateOnDemand = true;
                    treevCourses.Nodes.Add(node);
                }


                //select from topic where parentId = topicId.
                ds = myConClass.returnSqlDataset("select cd.courseName,ct.[date] from courseDetails cd join courseTimes ct on cd.courseId = ct.courseId");

                foreach (TreeNode treenode in treevCourses.Nodes)
                {
                    foreach (DataRow row in ds.Tables[0].Rows)
                    {
                        if (row["courseName"].ToString() == treenode.Value)
                        {
                            TreeNode node = new TreeNode(row["date"].ToString(), row["date"].ToString());
                            treenode.ChildNodes.Add(node);
                        }
                    }       
                }

Best Answer

There is no AutoPostBack property on TreeView. And as per the MSDN, The TreeNodeCheckChanged event is raised when a check box in the TreeView control changes state between posts to the server

You need to do something else, like mentioned on this link

1) Add on click attribute to TreeView1 on page load

protected void Page_Load(object sender, EventArgs e)
{
     TreeView1.Attributes.Add("onclick", "postBackByObject()");
}

2) add java script function and do the post back

    <script type="text/javascript">

     function postBackByObject()
     {
         var o = window.event.srcElement;
         if (o.tagName == "INPUT" && o.type == "checkbox")
        {
           __doPostBack("","");
        } 
    }
   </script>

3). Implement TreeNodeCheckChanged event

protected void TreeView1_TreeNodeCheckChanged(object sender, TreeNodeEventArgs e)
        {
            // do stuff
        }