How To Display Hierarchical Data in the DropDownList in ASP.NET

The GridView control is a feature-rich and versatile control used to accept, display, and edit data on a web page. It is a commonly used control in ASP.Net web applications. To use a GridView control, a DataSource control has to be attached to the GridView control.

In this post, I will demonstrate how you can use a simple ASP.NET DropDownList control to display hierarchical data.

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default6.aspx.cs" Inherits="Default6" %>

<!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>Untitled Page</title>
    <style type="text/css">
    .ParentChild
   
    {
    color:red;
   
    }
    </style>
</head>
<body>
    <form id="form1" runat="server">
        <div>
            <div>
                <asp:DropDownList ID="ddlCategories" runat="server" OnPreRender="ddlCategories_PreRender" />
                <asp:RequiredFieldValidator ID="rfvCategories" runat="server" ControlToValidate="ddlCategories"
                    InitialValue="Please select " ErrorMessage="Please select " />
                <br />
                <br />
                <asp:Button ID="Button1" runat="server" Text="Select" OnClick="Button1_Click" />
                <asp:Label ID="lblMessage" runat="server" />
            </div>
        </div>
    </form>
</body>
</html>
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class Default6 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            Bind();
        }
    }

    private void Bind()
    {
        DataSet ds = Data() as DataSet;

        foreach (DataRow row in ds.Tables[0].Rows)
        {

            int ParentID = Convert.ToInt32(row["ID"]);

            string ParentName = row["Name"] as String;

            ddlCategories.Items.Add(new ListItem(String.Empty, String.Empty));

            ddlCategories.Items.Add(new ListItem(ParentName, "0"));

            ddlCategories.Items.Add(new ListItem(String.Empty, String.Empty));

            DataRow[] childRows = ds.Tables[1].Select("ID = " + ParentID);

            foreach (DataRow childRow in childRows)
            {

                ddlCategories.Items.Add(new ListItem((string)childRow["Qul"], (childRow["ID"].ToString())));

            }

        }


    }
    private DataSet Data()
    {

        DataTable dt = new DataTable();
        dt.Columns.Add("Id", typeof(int));
        dt.Columns.Add("Name", typeof(string));

        dt.Rows.Add(new object[] { 1, "aaaa" });
        dt.Rows.Add(new object[] { 2, "bbbb" });
        dt.Rows.Add(new object[] { 3, "cccc" });
        dt.TableName = "Parent";




        DataTable dtc = new DataTable();
        dtc.Columns.Add("Id", typeof(int));
        dtc.Columns.Add("Qul", typeof(string));

        dtc.Rows.Add(new object[] { 1, "child1" });
        dtc.Rows.Add(new object[] { 1, "chid12" });
        dtc.Rows.Add(new object[] { 1, "child13" });
        dtc.Rows.Add(new object[] { 2, "child21" });
        dtc.Rows.Add(new object[] { 2, "child22" });
        dtc.Rows.Add(new object[] { 3, "child31" });
        dtc.Rows.Add(new object[] { 3, "child32" });
        dtc.Rows.Add(new object[] { 3, "child32" });
        dtc.TableName = "Child";


        DataSet ds = new DataSet();

        ds.Tables.Add(dt);
        ds.Tables.Add(dtc);
        Session["Parent"] = dt;

        return ds;

    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        {

            string selectedValue = String.Empty;



            if (Page.IsValid)
            {

                selectedValue = ddlCategories.SelectedValue;



                if (!String.IsNullOrEmpty(selectedValue) && selectedValue.Equals("0") == false)
                {

                    lblMessage.Text = "Selected Value is " + "#" + selectedValue;

                }

                else
                {

                    lblMessage.Text = "Please select a value";

                }

            }
        }
    }
    protected void ddlCategories_PreRender(object sender, EventArgs e)
    {
        foreach (ListItem item in ddlCategories.Items)
        {

            if (item.Value.Equals("0"))
            {

                item.Attributes.Add("class", "ParentChild");

            }

        }
    }
}

Please do not post any spam link in the comment box😊

إرسال تعليق (0)
أحدث أقدم