Wednesday, April 1, 2009
Dynamic tree view creation using c#
using System;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.IO;
using System.Data.SqlClient;
public partial class Default2 : System.Web.UI.Page
{
SqlCommand cmd;
SqlConnection cn;
//SqlDataReader dr;
protected void Page_Load(object sender, EventArgs e)
{
fill_Tree2();
}
void fill_Tree2()
{
DataSet PrSet = PDataset("Select productcode from ccc group by productcode");
TreeView1.Nodes.Clear();
foreach (DataRow dr in PrSet.Tables[0].Rows)
{
TreeNode tnParent = new TreeNode();
tnParent.Text = dr["productcode"].ToString();
//tnParent.Value = dr["ParentID"].ToString();
//tnParent.PopulateOnDemand = true;
TreeView1.Nodes.Add(tnParent);
FillChild(tnParent, tnParent.Value);
//FillChild1(tnParent, tnParent.Value);
tnParent.ToolTip = "Click to get Child";
//tnParent.SelectAction = TreeNodeSelectAction.SelectExpand;
tnParent.CollapseAll();
//tnParent.Selected = true;
}
}
public void FillChild(TreeNode parent, string ParentId)
{
DataSet ds = PDataset("Select * from ccc where productcode =" + ParentId+" order by productcode");
parent.ChildNodes.Clear();
foreach (DataRow dr in ds.Tables[0].Rows)
{
TreeNode child = new TreeNode();
child.Text = dr["productname"].ToString().Trim();
child.NavigateUrl = dr["url"].ToString().Trim();
//child.Value = dr["ChildId"].ToString().Trim();
if (child.ChildNodes.Count == 0)
{
child.PopulateOnDemand = true;
}
child.ToolTip = "Click to get Child";
child.SelectAction = TreeNodeSelectAction.SelectExpand;
child.CollapseAll();
parent.ChildNodes.Add(child);
}
}
//public void FillChild1(TreeNode parent, string ParentId)
//{
// DataSet ds = PDataset("Select * from ccc where productcode =" + ParentId);
// parent.ChildNodes.Clear();
// foreach (DataRow dr in ds.Tables[0].Rows)
// {
// TreeNode child1 = new TreeNode();
// child1.NavigateUrl = dr["url"].ToString().Trim();
// //child.Value = dr["ChildId"].ToString().Trim();
// if (child1.ChildNodes.Count == 0)
// {
// child1.PopulateOnDemand = true;
// }
// child1.ToolTip = "Click to get Child";
// child1.SelectAction = TreeNodeSelectAction.SelectExpand;
// child1.CollapseAll();
// parent.ChildNodes.Add(child1);
// }
//}
protected DataSet PDataset(string Select_Statement)
{
SqlConnection SqlCon = new SqlConnection("Data Source=.;Initial Catalog=govind;Integrated Security=True");
SqlDataAdapter ad = new SqlDataAdapter(Select_Statement, SqlCon);
DataSet ds = new DataSet();
ad.Fill(ds);
return ds;
}
protected void Button1_Click(object sender, EventArgs e)
{
cn = new SqlConnection("Data Source=.;Initial Catalog=govind;Integrated Security=True");
cmd= new SqlCommand("insert into ccc values('"+DropDownList1.SelectedItem.Text+"','"+TextBox1.Text+"','"+TextBox2.Text+"')",cn);
cmd.CommandType=CommandType.Text;
cn.Open();
cmd.ExecuteNonQuery();
fill_Tree2();
}
}
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment