استفاده از کنترل TreeView در ASP.Net

سه شنبه 17 شهریور 1394

در این مقاله نحوه استفاده از کنترل TreeView در ASP.Net را همراه با مثال در #C به شما آموزش می دهیم.

استفاده از کنترل TreeView در ASP.Net

دیتابیس

به منظور پر کردن treeview توسط رابطه والد و فرزندی دو جدول با نامهای  VehicleTypes و VehicleSubTypes ایجاد می کنیم. برای این مثال داده های زیر را در دو جدول زیر وارد کرده ایم.

VehicleTypes

VehicleSubTypes

 

زبان نشانه گذاری HTML

 کد HTML زیر شامل  ASP.Net TreeView می باشد. 

<h3>
    Vehicle Details</h3>
<hr />
<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>
 

فضای نام

فضاهای نام زیر را به پروژه خود اضافه  کنید.

#C

using System.Data;
using System.Data.SqlClient;
using System.Configuration;

VB.Net

Imports System.Data
Imports System.Data.SqlClient
Imports System.Configuration

پر کردن TreeView از دیتابیس

متد PopulateTreeView  یک متد بازگشتی می باشد. در رویداد  Page Load(لود صفحه)، TreeView با رکوردهایی از جدول  VehicleTypes، پر می شود. در این متد  یک حلقه روی  DataTable، اجرا می شود. و اگر ParentId صفر باشد به عنوان مثال گره، گره والد می باشد.  یک کوئری برای پرکردن گره فرزند مربوطه روی جدول VehicleSubTypes اجرا می شود و دوباره متد PopulateTreeView فراخوانی می شود. این روند تا زمانی که همه ی گره های والد همراه با گره های فرزندانشان به TreeView اضافه شود ادامه دارد.

#C

protected void Page_Load(object sender, EventArgs e)
{
    if (!this.IsPostBack)
    {
        DataTable dt = this.GetData("SELECT Id, Name FROM VehicleTypes");
        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["Name"].ToString(),
            Value = row["Id"].ToString()
        };
        if (parentId == 0)
        {
            TreeView1.Nodes.Add(child);
            DataTable dtChild = this.GetData("SELECT Id, Name FROM VehicleSubTypes WHERE VehicleTypeId = " + 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 = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
    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;
    }
}

VB.Net

Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
    If Not Me.IsPostBack Then
        Dim dt As DataTable = Me.GetData("SELECT Id, Name FROM VehicleTypes")
        Me.PopulateTreeView(dt, 0, Nothing)
    End If
End Sub
 
Private Sub PopulateTreeView(dtParent As DataTable, parentId As Integer, treeNode As TreeNode)
    For Each row As DataRow In dtParent.Rows
        Dim child As New TreeNode() With { _
         .Text = row("Name").ToString(), _
         .Value = row("Id").ToString() _
        }
        If parentId = 0 Then
            TreeView1.Nodes.Add(child)
            Dim dtChild As DataTable = Me.GetData("SELECT Id, Name FROM VehicleSubTypes WHERE VehicleTypeId = " + child.Value)
            PopulateTreeView(dtChild, Integer.Parse(child.Value), child)
        Else
            treeNode.ChildNodes.Add(child)
        End If
    Next
End Sub
 
Private Function GetData(query As String) As DataTable
    Dim dt As New DataTable()
    Dim constr As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
    Using con As New SqlConnection(constr)
        Using cmd As New SqlCommand(query)
            Using sda As New SqlDataAdapter()
                cmd.CommandType = CommandType.Text
                cmd.Connection = con
                sda.SelectCommand = cmd
                sda.Fill(dt)
            End Using
        End Using
        Return dt
    End Using
End Function

فایل های ضمیمه

برنامه نویسان

نویسنده 3355 مقاله در برنامه نویسان

کاربرانی که از نویسنده این مقاله تشکر کرده اند

در صورتی که در رابطه با این مقاله سوالی دارید، در تاپیک های انجمن مطرح کنید