Christ X

Christ X

  • NA
  • 33
  • 19.7k

problem OnSelectedNodeChanged event while page redirecting.

Oct 21 2013 6:39 AM
hi i have given my coding for treeview. everything works fine except OnSelectedNodeChanged event while page redirecting.


I) Defaultpage which runs by default.

II) Master page:

sitemaster.aspx page:
<asp:Panel ID ="pan" ScrollBars="Vertical" Width="270" runat="server">
     <asp:TreeView  
  ID="TreeView1" OnTreeNodePopulate="TreeView1_TreeNodePopulate"    HoverNodeStyle-ForeColor ="Red" RootNodeStyle-ForeColor="Red" OnSelectedNodeChanged="node_change"
  ExpandDepth="0" NodeWrap="true" BackColor="White" ForeColor="#5f6062" Font-Size="12px" style="font-family :Sans-Serif"  
  PopulateNodesFromClient="true" OnClick="OnExpandClick(event);"
  ShowLines="true" 
  ShowExpandCollapse="true" 
  runat="server" />
  </asp:Panel>

sitemaster aspx.cs

using System;
using System.Collections.Generic;
using System.Collections;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Text;

namespace Masterpagepopulatetreeview
{
    public partial class SiteMaster : System.Web.UI.MasterPage
    {
        
        protected void Page_Load(object sender, EventArgs e)
        {
if (!Page.IsPostBack)
                     {
                  
                    PopulateRootLevel();
                
                      }
          }
                 
     private void PopulateRootLevel()
        {
            SqlConnection objConn = new SqlConnection();
            objConn = Con.GetConnection();

            string sql = null;
            //  sql = "select a.id , a.descript,(select count(*) from table where parent = a.id )as childnodecount from tablename a where a.parent = 0 order by a.descript";";
           
            SqlCommand cmd = new SqlCommand(sql, objConn);
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            DataTable dt1 = new DataTable();
            da.Fill(dt1);
            PopulateNodes(dt1, TreeView1.Nodes);

        }

        private void PopulateNodes(DataTable dt, TreeNodeCollection nodes)
        {
            foreach (DataRow dr in dt.Rows)
            {
                TreeNode tn = new TreeNode();

                tn.Text = dr["descript"].ToString().Trim();

                tn.Value = dr["code_webcategory_id"].ToString().Trim();
                nodes.Add(tn);
               
                //if node has child nodes , then enable on-demand populating 
               tn.PopulateOnDemand = (Convert.ToInt32(dr["childnodecount"]) > 0);
            }

        
        }

        private void PopulateSubLevel(int parentid, TreeNode parentNode)
        {

            SqlConnection objConn = new SqlConnection();
            objConn = Con.GetConnection();

            string sql = null;

 sql = "select a.id , a.descript,(select count(*) from tablename where parent = a.id )as childnodecount from tablename a where a.parent =@parentid order by a.descript";

             SqlCommand cmd = new SqlCommand(sql, objConn);
           
            cmd.Parameters.Add("@parentID", SqlDbType.Int).Value = parentid;
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            DataTable dt = new DataTable();
            da.Fill(dt);      
            PopulateNodes(dt, nooo.ChildNodes);
           
            
        }
        
        protected void TreeView1_TreeNodePopulate(object sender, TreeNodeEventArgs  e ) 
        {
            
            PopulateSubLevel(Convert.ToInt32(e.Node.Value), e.Node);
           
        }

       
        protected void node_change(object sender, EventArgs  e)
        {
string s = TreeView1.SelectedNode.Value; 
Response.Redirect(string.Format("~/another.aspx?catid={0}", s.ToString()));
         }
       
    }
}


anther.aspx.cs
***********

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Masterpagepopulatetreeview.View
{
    public partial class another: System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Request.QueryString["catid"] != null)
            {
                string category_id_parentid = Request.QueryString["catid"].ToString();
               Response.Write(category_id_parentid);
            }
        }
    }
}


My Treeview works follow:
take an example : +Volleyball
step 1: first it binds rootlevel which is inside of page load.
step 2: when we click + in treeview , Page.IsPostBack will get false. so, It invokes "TreeView1_TreeNodePopulate". this captures current node value and node and send it to  PopulateSubLevel method which builds child for root level. likewise it goes any level.

TILL NOW EVERYTHING WORKS FINE.


here i want to redirect page to another.aspx page. here i am facing problem. 

so,  when i click on node (ie , Volleyball), it invokes "node_change". //step 1)now debugercontrol(cursor) in master.aspx.cs.

      protected void node_change(object sender, EventArgs  e)
        {
string s = TreeView1.SelectedNode.Value; 
Response.Redirect(string.Format("~/another.aspx?catid={0}", s.ToString()));//step2:) now debuggercontrol in another.aspx page.
         }
Now, 3) Again the debuggercontrol come back to masterpage.aspx.cs.

If you see now, the master.aspx.cs is got referesh. so, it goes inside of page_load event. just invokes PopulateRootLevel();

now we will get rootlevel tree alone in the another page.

This is the problem. If anybody got the solution. lead me how to solve this problem. i tried with saving node in session and some other way from site. 

somebody given solution Treeviewcancelevent,mouse clickevent which is windows application. but This treeview is developed web application, not able to try and do. please modify my code.

Thanks