iranidesign
کاربر تازه وارد
- تاریخ عضویت
- 3 جولای 2006
- نوشتهها
- 62
- لایکها
- 0
سلام
میخوام tree view رو به db وصل کنم در net2. !!!
چطوری این کار رو انجام بدم؟
میخوام tree view رو به db وصل کنم در net2. !!!
چطوری این کار رو انجام بدم؟
protected void Page_Load(object sender, EventArgs e)
{
if (!User.Identity.IsAuthenticated || !User.IsInRole("admin"))
Panel1.Visible = false;
if (Request.QueryString["StartID"] != null)
{
PopulateStartLevel(Request.QueryString["StartID"]);
return;
}
if (!IsPostBack)
PopulateStartLevel(User.Identity.Name);
// PopulateStartLevel("1");
//Response.Write(DatabaseWorker.GetSubChilds("1") +"<br>");
DatabaseWorker.LogRegister(User.Identity.Name, "View Tree in " + DateTime.Now);
}
protected void PopulateStartLevel(string StartNodeID)
{
SqlConnection Cnn = new SqlConnection(@"Data Source=BROOKLYN;Initial Catalog=AzPaygah;Integrated Security=True");
string query = "select UserName,FirstName,Family,(select count(*) FROM TUser WHERE ParentID=sc.UserName) AS childnodecount " +
"FROM TUser as sc where UserName = @UserName";// parentID =@ParentID";
SqlCommand cmd = new SqlCommand(query, Cnn);
cmd.Parameters.Add("@UserName", SqlDbType.NVarChar).Value = StartNodeID;
SqlDataAdapter objDa = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
objDa.Fill(dt);
PopulateNodes(dt, MainTree.Nodes);
}
protected void PopulateNodes(DataTable dt, TreeNodeCollection nodes)
{
foreach (DataRow dr in dt.Rows)
{
TreeNode tn = new TreeNode();
tn.Text = dr["FirstName"].ToString() + " "+ dr["Family"].ToString();
tn.Value = dr["UserName"].ToString();
nodes.Add(tn);
tn.PopulateOnDemand = (Convert.ToInt32(dr["childnodecount"]) > 0);
}
}
protected void MainTree_TreeNodePopulate(object sender, TreeNodeEventArgs e)
{
PopulateSubLevel(e.Node.Value, e.Node);
}
protected void PopulateSubLevel(string ParentID, TreeNode ParentNode)
{
SqlConnection Cnn = new SqlConnection(@"Data Source=BROOKLYN;Initial Catalog=AzPaygah;Integrated Security=True");
string query = "select UserName,FirstName,Family, (select count(*) FROM TUser WHERE ParentID=sc.UserName) AS childnodecount " +
"FROM TUser as sc where parentID ='" + ParentID + "'";
SqlCommand objCommand = new SqlCommand(query, Cnn);
SqlDataAdapter objDa = new SqlDataAdapter(objCommand);
DataTable dt = new DataTable("dt");
objDa.Fill(dt);
PopulateNodes(dt, ParentNode.ChildNodes);
}