when i click on the treeview the page gets refresh and the control wont go to roottree_SelectedNodeChanged

Plz anyone help me......

/* Document */
    private void GenerateMarksTree(StudentManagement.Student stud)
    {

        TreeView roottree = new TreeView();


        roottree.ForeColor = Color.Black;
        roottree.ShowLines = true;

        TreeNode root = new TreeNode();
        root.Text = stud.Branch.ToString();
        root.Value = "";
        roottree.ShowLines = true;
        roottree.ForeColor = Color.Black;
        roottree.Font.Bold = true;
        roottree.Font.Size = 10;
        roottree.Nodes.Add(root);


        StudentManagement.StudentManagementSoap Service2 = new StudentManagement.StudentManagementSoapClient();
        GetClassDetailsRequest request2 = new GetClassDetailsRequest();
        request2.Body = new GetClassDetailsRequestBody();
        request2.Body.id = stud.StudentId;
        GetClassDetailsResponse response2 = Service2.GetClassDetails(request2);
        foreach (StudentClassDetails studentdetails in response2.Body.GetClassDetailsResult)
        {
            TreeNode childNode = new TreeNode();
            childNode.Value = "";
            childNode.Text = "Semester: " + studentdetails.ClassId.ToString(); //sem.SemistarId.ToString();
            root.ChildNodes.Add(childNode);

            StudentManagement.StudentManagementSoap studentService = new StudentManagement.StudentManagementSoapClient();
            GetExamListRequest studentrequest = new GetExamListRequest();
            studentrequest.Body = new GetExamListRequestBody();
            studentrequest.Body.studentdetails = studentdetails;
            GetExamListResponse studentresponse = studentService.GetExamList(studentrequest);

            foreach (Examination examList in studentresponse.Body.GetExamListResult)
            {

                TreeNode childNode1 = new TreeNode();
                //childNode1.SelectAction = new EventHandler(roottree_SelectedNodeChanged); 
                childNode1.Value = examList.Exam_Id.ToString();
                childNode1.Text = examList.Exam_Name + "" + examList.Start_Date.ToShortDateString();
                childNode.ChildNodes.Add(childNode1);
            }
            childNode.Expanded = false;
        }
        Panelleft1.Controls.Clear();
        Panelleft1.Controls.Add(roottree);
        //roottree.SelectedValue += new EventHandler(roottree_SelectedNodeChanged);
        roottree.SelectedNodeChanged += new EventHandler(roottree_SelectedNodeChanged);

        //roottree.SelectedNodeChanged += new EventHandler(roottree_SelectedNodeChanged);
    }


    protected void roottree_SelectedNodeChanged(object sender, EventArgs e)
    {
        TreeView treeView = (TreeView)sender;
        if (treeView.SelectedValue != null && treeView.SelectedValue != "")
        {
            if (Session["User"] != null)
            {
                User user = (User)Session["User"];
                StudentManagementSoap service = new StudentManagementSoapClient();
                GetMarksListByIdRequest request = new GetMarksListByIdRequest();
                request.Body = new GetMarksListByIdRequestBody();
                request.Body.examId = treeView.SelectedValue;
                request.Body.id = user.TypeOfUserId;
                GetMarksListByIdResponse response = service.GetMarksListById(request);
                DataSet ds = new DataSet("DS");
                DataTable dt = ds.Tables.Add("Result");
                dt.Columns.Add("Subject Name", typeof(string));
                dt.Columns.Add("Maximum Marks", typeof(int));
                dt.Columns.Add("Marks Obtained", typeof(int));
                dt.Columns.Add("Result", typeof(string));

                foreach (Marks mk in response.Body.GetMarksListByIdResult)
                {
                    DataRow dr = dt.NewRow();

                    dr["Subject Name"] = mk.SubjectName.ToString();
                    dr["Maximum Marks"] = int.Parse(mk.MaximumMarks.ToString());
                    dr["Marks Obtained"] = int.Parse(mk.MarksObtained.ToString());
                    dr["Result"] = mk.Result.ToString();

                    dt.Rows.Add(dr);
                }
                GridviewMarks.Font.Bold = true;
                GridviewMarks.Font.Size = 12;
                GridviewMarks.Attributes.Add("Align", "left");

                GridviewMarks.DataSource = ds;
                GridviewMarks.DataBind();


            }
        }
    }

Dani AI

Generated

Most likely cause: the TreeView is being created dynamically but not rebuilt early enough (or not given a stable ID) on postback, so ASP.NET never wires the control state/events and the click only posts back the page. 's tiny test proves the event will fire when the control exists on postback; the real fix is to recreate the same control tree and reattach the SelectedNodeChanged handler on every request, and do it early in the page lifecycle.

Key points and common mistakes seen in this thread:

  • Give the TreeView a deterministic ID and recreate it with the same IDs on every postback.
  • Wire the SelectedNodeChanged handler when you create the control (and do this on every request).
  • Do not try to assign an event handler to SelectActionSelectAction is an enum (use the enum value that allows selection, e.g. Select or SelectExpand) so that clicking a node results in selection.
  • Avoid relying on creating the tree only inside a if (!IsPostBack) block unless you persist the node structure some other way; that advice from is misleading for dynamically created controls.

Quick troubleshooting checklist:

  • Recreate the tree in Page_Init (or at least before viewstate restoration) and attach the event handler there.
  • Ensure viewstate is enabled for the TreeView.
  • Set node SelectAction to allow selection for nodes you want clickable.
  • If inside an UpdatePanel, register the TreeView for async postbacks or add the proper trigger.
  • Place breakpoints in Page_Init and in SelectedNodeChanged, and inspect the post data (the page’s __EVENTTARGET) to confirm the TreeView is posting back.

If these fixes are applied the SelectedNodeChanged event will trigger as in ’s example; if it still fails, rebuild the smallest possible page that reproduces the problem and verify the control is present in the control tree at the start of the postback.

Recommended Answers

All 4 Replies

Unfortunately that is a lot of code that we can't compile. Can you upload a sample project with the data embedded?

i tried it my self nd its working, i tried:

TreeView trVw = new TreeView();
        trVw.Nodes.Add(new TreeNode("One"));
        trVw.Nodes.Add(new TreeNode("Two"));
        trVw.SelectedNodeChanged += new EventHandler(trVw_SelectedNodeChanged);
        this.pnl.Controls.Add(trVw);


    void trVw_SelectedNodeChanged(object sender, EventArgs e)
    {
        throw new NotImplementedException();
        //put break point here and it worked
    }

maybe you have a postback event on the control that contains the TreeView try to check it out and if not try to sned me the solution and i will check it for you.

address me in private and i will give you my email

You can check below two things:
1. Check that populate treeview only on load time. Means wrap with !ispostback .
2. Stop running the application open the page in design view check events from property pane that your desired event attached.

debug........!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.