01.using System.Data.SqlClient;
02.
03.public partial class _Default : System.Web.UI.Page
04.{
05. static string connstr = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString.ToString();
06. static SqlConnection con = new SqlConnection(connstr);
07.
08. protected void Page_Load(object sender, EventArgs e)
09. {
10. if (Page.IsPostBack==false)
11. {
12.
13. SqlDataAdapter daroot = new SqlDataAdapter("select * from rootmenu",con);
14. DataSet ds = new DataSet();
15. daroot.Fill(ds, "rootmenu");
16.
17. foreach (DataRow parentrow in ds.Tables["rootmenu"].Rows)
18. {
19. TreeNode parentnode = new TreeNode();
20. parentnode.Text=parentrow["name"].ToString();
21. int parentid = Int32.Parse(parentrow["idrootmenu"].ToString());
22.
23. string[] childtext=getchildtext(parentid);
24. for (int i = 0; i < childtext.Length; i++)
25. {
26. TreeNode childnode = new TreeNode();
27. childnode.Text = childtext[i].ToString();
28. childnode.NavigateUrl = "~/default2.aspx";
29. parentnode.ChildNodes.Add(childnode);
30. }
31.
32. TreeView1.Nodes.Add(parentnode);
33. }
34. }
35.
36.
37.
38. }
39.
40.
41. /// <summary>
42. /// وظیفه برگرداندن یک آرایه به منظور نسبت دادن به خاصیت
43. /// text
44. /// شی
45. ///childnode
46. ///در قطعه کد بالا
47. /// </summary>
48. /// <param name="parentid">مقدار صحیح</param>
49. /// <returns></returns>
50.
51. public string[] getchildtext(int parentid)
52. {
53.
54. TreeNode childnode=new TreeNode();
55. SqlDataAdapter da = new SqlDataAdapter("select * from submenu where fidroot="+parentid+" ",con);
56. DataTable dt = new DataTable();
57. da.Fill(dt);
58. int count=0;
59. string[] childname=new string[dt.Rows.Count];
60.
61.
62. for (int i = 0; i < dt.Rows.Count; i++)
63. {
64. count += count + 1;
65. childname[i] = dt.Rows[i]["namesubmenu"].ToString();
66.
67. }
68. return childname;
69.
70. }
71.
72.
73.
74. }