در این مقاله به TreeView رفتیم ، مشکلی که خودم مدت ها باهاش درگیر بودم و در بیشتر پروژه ها خیلی از راه ها استفاده کردم که TreeView ساده ترین و بهترین روش های است که میتونید یک لیست را نمایش بدید ، این آموزش در محیط ASP.NET و زبان سی شارپ و دیتابیس SQL آماده شده است ، نمونه سورس نیز براتون قرار دادم که به راحتی آموزش را درک کنید.پیش از مقاله آموزش و نمونه سورس های زیادی از TreeView منتشر کرده ایم که میتوانید استفاده کنید.
در زیر طراحی جدول داده های مرا میبینید :
تصویر ۱
در زیر رکورد های داخل جدول داده های مرا میبینید :
در اینجا من رابطه ی بین کارمندان و سرپرست کارمندان را داریم.
در زیر کد aspx مرا میتوانید ببینید :
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title>Tree View Example in ASP.NET C#</title> </head> <body> <form id="form1" runat="server"> <table cellpadding="4" cellspacing="4" width="50%" align="center" style="background-color: SkyBlue;"> <tr> <td align="center" style="font-family: Times New Roman; font-size: 14pt; color: Green;"> <h3> Team Leader and their Team Employee Information</h3> </td> </tr> <tr> <td style="background: #F5F5F5; padding-left: 30px;"> <asp:TreeView ID="TreeView1" runat="server" ImageSet="XPFileExplorer" NodeIndent="15"> <HoverNodeStyle Font-Underline="True" ForeColor="#6666AA" /> <NodeStyle Font-Names="Tahoma" Font-Size="8pt" ForeColor="Black" HorizontalPadding="2px" NodeSpacing="0px" VerticalPadding="2px"></NodeStyle> <ParentNodeStyle Font-Bold="False" /> <SelectedNodeStyle BackColor="#B5B5B5" Font-Underline="False" HorizontalPadding="0px" VerticalPadding="0px" /> </asp:TreeView> </td> </tr> </table> </form> </body> </html>
حال کد aspx.cs :
using System; using System.Configuration; using System.Data; using System.Linq; 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.Xml.Linq; using System.Data.SqlClient; using System.Configuration; public partial class _Default : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { if (!this.IsPostBack) { DataTable dt = this.GetData("SELECT EmployeeCode, EmployeeName FROM Employee Where EmployeeSupervisorCode=0"); this.PopulateTreeView(dt, 0, null); } } private void PopulateTreeView(DataTable dtParent, int parentId, TreeNode treeNode) { foreach (DataRow row in dtParent.Rows) { TreeNode child = new TreeNode { Text = row["EmployeeName"].ToString(), Value = row["EmployeeCode"].ToString() }; if (parentId == 0) { TreeView1.Nodes.Add(child); DataTable dtChild = this.GetData("SELECT EmployeeCode, (EmployeeName+ ', '+ Experience + ', '+ Address) As EmployeeName FROM Employee WHERE EmployeeSupervisorCode = " + child.Value); PopulateTreeView(dtChild, int.Parse(child.Value), child); } else { treeNode.ChildNodes.Add(child); } } } private DataTable GetData(string query) { DataTable dt = new DataTable(); string constr = @"Server=MyPC\SqlServer2k8;database=Test;Integrated Security=true;"; using (SqlConnection con = new SqlConnection(constr)) { using (SqlCommand cmd = new SqlCommand(query)) { using (SqlDataAdapter sda = new SqlDataAdapter()) { cmd.CommandType = CommandType.Text; cmd.Connection = con; sda.SelectCommand = cmd; sda.Fill(dt); } } return dt; } } }
حال برنامه را اجرا کنید :
این روش کارآیی پایینی داره. تعداد رفت و برگشتهای اون بی اندازه زیاد هست. به همین جهت مباحث CTE مطرح شده.
۵
موافقم مهندس عزیز ! در بعضی از مواقع در پنل مدیر میشه ازش استفاده کرد.
۷