Description:-
In
previous example we explain that how to bind Directory and sub Directory in
Hierchical format in Treeview control in asp.net.but now in this example we
explain that how to Bind directory and sub-directory in DropdownList with
Hierchy format like display in treeview in asp.net or display hierchical of
directory in dropdownlist.
Suppose you have one table in which country and
state are dtored so that in dropdownlist you have to bind country with it’s
state in hierchical format like so above image.
for binding hierchy of data in dropdown first you have to create a one table Folder_Master like below
How to handle Concurrency in Linq to Sql Concurrency Example
Folder.cs:-
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
/// <summary>
/// Summary description for Folder
/// </summary>
public class Folder
{
public string Folder_Name { get; set; }
public int Folder_ID { get; set; }
public int Sub_FolderId { get; set; }
public int Company_ID { get; set; }
}
hierchyindropdown.aspx:-
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="hierchyindropdown.aspx.cs" Inherits="hierchyindropdown" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox><br />
<asp:DropDownList ID="DropDownList1" runat="server"
DataTextField="Folder_Name" DataValueField="Folder_ID" ></asp:DropDownList>
<br />
<br />
<div style="overflow:scroll">
</div>
<asp:Button ID="Button1" runat="server" Text="Add Directory To this
hierchy" OnClick="Button1_Click" />
</div>
</form>
</body>
</html>
hierchyindropdown.aspx.cs:-
using
System;
using
System.Collections.Generic;
using
System.Linq;
using
System.Web;
using
System.Collections;
using
System.Data;
using
System.Data.SqlClient;
using
System.Web.UI.WebControls;
public partial class hierchyindropdown : System.Web.UI.Page
{
int cnt = 0;
private static string Config = "Data Source=SQLDB;User
ID=DMSh;Password=Dms#5";
List<Folder> list = new List<Folder>();
List<Folder> sublist = new List<Folder>();
List<Folder> sublist1 = new List<Folder>();
protected void Page_Load(object
sender, EventArgs e)
{
if (!Page.IsPostBack)
{
showSubGroup(0, 1);
DropDownList1.DataSource = sublist;
DropDownList1.DataBind();
}
}
private void showSubGroup(int underGrpCode, int level)
{
list.Clear();
SqlConnection db = new SqlConnection(Config);
string query = "select * from
Folder_Master where Sub_FolderId=" + underGrpCode;
SqlCommand cmd = new SqlCommand(query,
db);
db.Open();
using (SqlDataReader dataReader = cmd.ExecuteReader())
{
while (dataReader.Read())
{
cnt++;
Folder obj = new Folder();
if (dataReader["Folder_ID"] != DBNull.Value)
{
if (dataReader["Folder_ID"] != DBNull.Value)
{ obj.Folder_ID = (int)dataReader["Folder_ID"]; }
if (dataReader["Sub_FolderId"] != DBNull.Value)
{ obj.Sub_FolderId = (int)dataReader["Sub_FolderId"]; }
if (dataReader["Folder_Name"] != DBNull.Value)
{ obj.Folder_Name = (string)dataReader["Folder_Name"]; }
if (dataReader["Company_ID"] != DBNull.Value)
{ obj.Company_ID = (int)dataReader["Company_ID"]; }
list.Add(obj);
foreach (var row in list.ToList())
{
int Sub_FolderId = row.Sub_FolderId;
int grpCode = row.Folder_ID;
string Folder_Name = new string('-', level) + row.Folder_Name;
Folder obj1 = new Folder();
obj1.Folder_Name = "|" + Folder_Name;
obj1.Folder_ID = grpCode;
sublist.Add(obj1);
if (list.Count > 0)
showSubGroup(list[0].Folder_ID, level + 3);
}
}
}
}
}
protected void Button1_Click(object sender, EventArgs e)
{
string Config = "Data
Source=SQLDB;User ID=DMSh;Password=Dms#5";
SqlConnection db = new SqlConnection(Config);
//
Database db = new SqlDatabase(Config);
string query = "insert into
Folder_Master(Folder_Name,Sub_FolderId) values('" + TextBox1.Text + "','" + DropDownList1.SelectedItem.Value + "')";
SqlCommand cmd = new SqlCommand(query,
db);
db.Open();
cmd.ExecuteNonQuery();
showSubGroup(0, 1); //caaling
method for listing new deirectory was added in hierchy
DropDownList1.DataSource = sublist;
DropDownList1.DataBind();
}
}
0 comments:
Post a Comment