Back after one year of CHANGE !!!
A few days ago one of my friend had a question about tree node, he wanted to copy the text of selected node else the app should not copy. So I did some R&D on events available with tree node. Its the
OnSelectedNodeChanged..
Here is sample code for the same :)
*********ASPX Code****************
***************************************
*****Code behind***********
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
Bind_tree();
}
}
public void Bind_tree()
{
DataTable dt = Get_District();
for (int i = 0; i < dt.Rows.Count; i++)
{
TreeNode no = new TreeNode();
no.Text = dt.Rows[i]["District"].ToString();
no.Value = dt.Rows[i]["District"].ToString();
Get_allNodes(no);
this.TreeView1.Nodes.Add(no);
}
}
public void Get_allNodes(TreeNode no)
{
DataTable dt = Get_Arealist();
DataRow[] rows = dt.Select("District='" + no.Value + "'");
for (int i = 0; i < rows.Length; i++)
{
TreeNode child = new TreeNode();
child.Text = rows[i]["Area"].ToString();
child.Value = rows[i]["Area"].ToString();
no.ChildNodes.Add(child);
Get_allNodes(child);
}
}
// You can get the data from your own database.
// I get the datatable like this in order to run the sample more conveniently.
public DataTable Get_District()
{
DataTable dt = new DataTable();
DataRow dr;
string Name = "District_1,District_2,District_3";
string ID = "1,2,3";
string[] list1 = Name.Split(',');
string[] list2 = ID.Split(',');
dt.Columns.Add(new DataColumn("District"));
dt.Columns.Add(new DataColumn("ID"));
for (int i = 0; i < list1.Length; i++)
{
dr = dt.NewRow();
dr["District"] = list1[i];
dr["ID"] = list2[i];
dt.Rows.Add(dr);
}
return dt;
}
// You can get the data from your own database.
// I get the datatable like this in order to run the sample more conveniently.
public DataTable Get_Arealist()
{
DataTable dt = new DataTable();
DataRow dr;
string Name = "Area_1,Area_2,Area_3,Area_4,Area_5";
string Dis = "District_1,District_1,District_1,District_3,Area_1";
string ID = "1,2,3,4,5";
string[] list1 = Name.Split(',');
string[] list2 = Dis.Split(',');
string[] list3 = ID.Split(',');
dt.Columns.Add(new DataColumn("Area"));
dt.Columns.Add(new DataColumn("District"));
dt.Columns.Add(new DataColumn("ID"));
for (int i = 0; i < list1.Length; i++)
{
dr = dt.NewRow();
dr["Area"] = list1[i];
dr["District"] = list2[i];
dr["ID"] = list3[i];
dt.Rows.Add(dr);
}
return dt;
}
protected void TreeView1_TreeNodeExpanded(object sender, TreeNodeEventArgs e)
{
TreeNodeCollection ts = null;
if (e.Node.Parent == null)
{
ts = ((TreeView)sender).Nodes;
}
else
ts = e.Node.Parent.ChildNodes;
foreach (TreeNode node in ts)
{
if (node == e.Node)
{
node.Expand();
}
else
{
node.Collapse();
}
}
}
protected void TreeView1_SelectedNodeChanged(object sender, EventArgs e)
{
TreeNodeEventArgs xm = new TreeNodeEventArgs(this.TreeView1.SelectedNode);
TreeView1_TreeNodeExpanded(sender, xm);
string value = this.TreeView1.SelectedNode.Text;
this.Label1.Text = value;
Page.ClientScript.RegisterStartupScript(this.GetType(), "Ming", "Getvalue('" + value + "')", true);
}
Happy Coding :)