Arkadaşlar merhaba veritabanından treeview'e veri çekmek, treeview'e yeni node eklemek, node silmek ve node güncellemek ile alakalı bir ihtiyacınız olursa aşağıdaki kodları kullanabilirsiniz.
-------------------------------------------------------------------------------------------------------
[b]VERİ TABANINDA AŞAĞIDAKİ GİBİ TABLO OLUŞTURALIM[/b]
CREATE TABLE tblKategori
(
ID int identity primary key,
KategoriAdi nvarchar(50),
Link nvarchar(50),
UstKategoriId int references tblKategori (ID)
)
[b]BASİT BİR SELECT PROCESURU YAZALIM[/b]
create procedure spKategoriAl
as
begin
select ID, KategoriAdi, Link, UstKategoriId from tblEmployee
end
---------------------------------------------------------------------------------------------------
[b]BENİM TASARIMIM AŞAĞIDAKİ GİBİ[/b]
<%@ Page Title="" Language="C#" MasterPageFile="~/MainMasterPage.master" AutoEventWireup="true" CodeFile="treeviewdeneme.aspx.cs" Inherits="treeviewdeneme" %>
<asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server" >
<style type="text/css">
.auto-style6 {
width: 100%;
vertical-align:top;
}
.style1{
vertical-align:top;
}
.auto-style7 {
vertical-align: top;
width: 53px;
}
.auto-style8 {
width: 100%;
}
.auto-style9 {
width: 80px;
}
.auto-style10 {
float: left;
width: 104px;
}
</style>
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
<table width="49%" cellpadding="0" cellspacing="4" style="float:left">
<tr>
<td style="background-color: #FFFFFF">
<asp:TreeView ID="TreeView1" runat="server" ExpandDepth="0" ShowCheckBoxes="All" Font-Bold="True" ForeColor="#5D7B9D" Width="241px" OnSelectedNodeChanged="TreeView1_SelectedNodeChanged" OnTreeNodeCheckChanged="TreeView1_TreeNodeCheckChanged" ShowLines="True">
<LeafNodeStyle Font-Italic="True" />
</asp:TreeView>
</td>
</tr>
</table>
<table height="100%" width="2%" cellpadding="0" cellspacing="4" style="float:left">
<tr>
<td style="background-color: #5D7B9D">
</td>
</tr>
</table>
<table width="14%" cellpadding="0" cellspacing="4" style="float:left">
<tr>
<td>
<asp:Button ID="btnEkle" runat="server" Font-Bold="True" ForeColor="#5D7B9D" OnClick="btnEkle_Click" Text="Ekle" Width="70px" />
</td>
</tr>
<tr>
<td>
<asp:Button ID="btnKaldir" runat="server" Font-Bold="True" ForeColor="#5D7B9D" OnClick="btnKaldir_Click" Text="Kaldır" Width="70px" />
</td>
</tr>
<tr>
<td>
<asp:Button ID="btnGuncelle" runat="server" Font-Bold="True" ForeColor="#5D7B9D" OnClick="btnGuncelle_Click" Text="Güncelle" Width="70px" />
</td>
</tr>
</table>
<table width="35%" cellpadding="0" cellspacing="4" style="float:left">
<tr>
<td class="auto-style9">
<asp:Label ID="lblId" runat="server" Font-Bold="True" ForeColor="#5D7B9D" Text="ID:" Visible="False"></asp:Label>
</td>
<td class="auto-style10">
<asp:Label ID="labelId" runat="server" Font-Bold="True" ForeColor="#5D7B9D" Visible="False"></asp:Label>
</td>
</tr>
<tr>
<td class="auto-style9">
<asp:Label ID="lblBaslik" runat="server" Font-Bold="True" ForeColor="#5D7B9D" Text="Üst Başlık:" Visible="False"></asp:Label>
</td>
<td class="auto-style10">
<asp:TextBox ID="txtBaslik" runat="server" Font-Bold="True" ForeColor="#5D7B9D" Width="125px" Enabled="False" Visible="False"></asp:TextBox>
</td>
</tr>
<tr>
<td class="auto-style9">
<asp:Label ID="lblYeniBaslik" runat="server" Font-Bold="True" ForeColor="#5D7B9D" Text="Başlık:" Visible="False"></asp:Label>
</td>
<td class="auto-style10">
<asp:TextBox ID="txtYeniBaslik" runat="server" Font-Bold="True" ForeColor="#5D7B9D" Width="125px" autocomplete="off" Visible="False"></asp:TextBox>
</td>
</tr>
<tr>
<td class="auto-style9">
</td>
<td class="auto-style10">
<asp:Button ID="btnKaydet" runat="server" Font-Bold="True" ForeColor="#5D7B9D" Text="Kaydet" Width="75px" OnClick="btnKaydet_Click" Visible="False" />
<asp:Button ID="btnSil" runat="server" Font-Bold="True" ForeColor="#5D7B9D" Text="Sil" Width="75px" OnClick="btnSil_Click" Visible="False" />
<asp:Button ID="btnGuncelle2" runat="server" Font-Bold="True" ForeColor="#5D7B9D" Text="Güncelle" Width="75px" OnClick="btnGuncelle2_Click" Visible="False" />
</td>
</tr>
</table>
</td>
</tr>
</table>
</asp:Content>
-------------------------------------------------------------------------------------------------------
[b]CS KODLARI İSE AŞAĞIDAKİ GİBİDİR[/b]
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;
using System.Web.Configuration;
using System.Data.SqlClient;
using System.Data;
public partial class treeviewdeneme : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
GetTreeViewItems();
}
}
private void temizle()
{
lblBaslik.Visible = false;
lblId.Visible = false;
lblYeniBaslik.Visible = false;
txtBaslik.Visible = false;
labelId.Visible = false;
txtYeniBaslik.Visible = false;
btnKaydet.Visible = false;
btnGuncelle2.Visible = false;
btnSil.Visible = false;
}
private void GetTreeViewItems()
{
string cs = ConfigurationManager.ConnectionStrings["DbBaglanti"].ConnectionString;
SqlConnection con = new SqlConnection(cs);
SqlDataAdapter da = new SqlDataAdapter("spKategoriAl", con);
DataSet ds = new DataSet();
da.Fill(ds);
ds.Relations.Add("ChildRows", ds.Tables[0].Columns["ID"], ds.Tables[0].Columns["UstKategoriId"]);
foreach (DataRow level1DataRow in ds.Tables[0].Rows)
{
if (string.IsNullOrEmpty(level1DataRow["UstKategoriId"].ToString()))
{
TreeNode parentTreeNode = new TreeNode();
parentTreeNode.Text = level1DataRow["KategoriAdi"].ToString();
parentTreeNode.Value = level1DataRow["ID"].ToString();
parentTreeNode.NavigateUrl = level1DataRow["Link"].ToString();
GetChildRows(level1DataRow, parentTreeNode);
TreeView1.Nodes.Add(parentTreeNode);
}
}
}
private void GetChildRows(DataRow DataRow, TreeNode treeNode)
{
DataRow[] childRows = DataRow.GetChildRows("ChildRows");
foreach (DataRow childRow in childRows)
{
TreeNode childTreeNode = new TreeNode();
childTreeNode.Text = childRow["KategoriAdi"].ToString();
childTreeNode.Value = childRow["ID"].ToString();
childTreeNode.NavigateUrl = childRow["Link"].ToString();
treeNode.ChildNodes.Add(childTreeNode);
if (childRow.GetChildRows("ChildRows").Length > 0)
{
GetChildRows(childRow, childTreeNode);
}
}
}
private void GetSelectedTreeNodes(TreeNode parentTreeNode)
{
if (parentTreeNode.Checked)
{
labelId.Text = parentTreeNode.Value;
txtBaslik.Text = parentTreeNode.Text;
}
if (parentTreeNode.ChildNodes.Count > 0)
{
foreach (TreeNode childTreeNode in parentTreeNode.ChildNodes)
{
GetSelectedTreeNodes(childTreeNode);
}
}
}
protected void TreeView1_SelectedNodeChanged(object sender, EventArgs e)
{
GetSelectedTreeNodes(TreeView1.Nodes[0]);
}
protected void TreeView1_TreeNodeCheckChanged(object sender, TreeNodeEventArgs e)
{
GetSelectedTreeNodes(TreeView1.Nodes[0]);
}
protected void btnKaydet_Click(object sender, EventArgs e)
{
string cs = ConfigurationManager.ConnectionStrings["DbBaglanti"].ConnectionString;
SqlConnection con = new SqlConnection(cs);
con.Open();
SqlCommand sorgu = new SqlCommand("INSERT INTO tblKategori (KategoriAdi, UstKategoriId) VALUES (@txt,@id)", con);
sorgu.Parameters.AddWithValue("@id", labelId.Text);
sorgu.Parameters.AddWithValue("@txt", txtYeniBaslik.Text);
sorgu.ExecuteNonQuery();
Page.ClientScript.RegisterStartupScript(this.GetType(), "Scripts", "<script>alert('Kaydedildi!!!')</script>");
con.Close();
TreeView1.Nodes.Clear();
GetTreeViewItems();
//Page.Response.Redirect(Page.Request.Url.ToString());
TreeView1.ExpandAll();
temizle();
}
protected void btnEkle_Click(object sender, EventArgs e)
{
GetSelectedTreeNodes(TreeView1.Nodes[0]);
lblId.Visible = true;
lblBaslik.Visible = true;
lblYeniBaslik.Visible = true;
labelId.Visible = true;
txtBaslik.Visible = true;
txtYeniBaslik.Visible = true;
btnKaydet.Visible = true;
btnSil.Visible = false;
btnGuncelle2.Visible = false;
txtBaslik.Enabled = false;
lblBaslik.Text = "Üst Başlık:";
}
protected void btnKaldir_Click(object sender, EventArgs e)
{
GetSelectedTreeNodes(TreeView1.Nodes[0]);
lblId.Visible = true;
lblBaslik.Visible = true;
labelId.Visible = true;
txtBaslik.Visible = true;
btnSil.Visible = true;
btnKaydet.Visible = false;
btnGuncelle2.Visible = false;
lblBaslik.Text = "Başlık:";
lblYeniBaslik.Visible = false;
txtYeniBaslik.Visible = false;
txtBaslik.Enabled = false;
}
protected void btnSil_Click(object sender, EventArgs e)
{
string cs = ConfigurationManager.ConnectionStrings["DbBaglanti"].ConnectionString;
SqlConnection con = new SqlConnection(cs);
con.Open();
SqlCommand sorgu = new SqlCommand("delete from tblKategori where ID = @id", con);
sorgu.Parameters.AddWithValue("@id", labelId.Text);
sorgu.ExecuteNonQuery();
// Response.Write("Kaydedildi!!!");
Page.ClientScript.RegisterStartupScript(this.GetType(), "Scripts", "<script>alert('Başlık Kaldırıldı!!!')</script>");
con.Close();
TreeView1.Nodes.Clear();
GetTreeViewItems();
TreeView1.ExpandAll();
temizle();
}
protected void btnGuncelle_Click(object sender, EventArgs e)
{
GetSelectedTreeNodes(TreeView1.Nodes[0]);
lblId.Visible = true;
labelId.Visible = true;
lblBaslik.Visible = true;
txtBaslik.Visible = true;
txtBaslik.Enabled = true;
btnSil.Visible = false;
btnKaydet.Visible = false;
btnGuncelle2.Visible = true;
lblBaslik.Text = "Başlık:";
lblYeniBaslik.Visible = false;
txtYeniBaslik.Visible = false;
}
protected void btnGuncelle2_Click(object sender, EventArgs e)
{
string cs = ConfigurationManager.ConnectionStrings["DbBaglanti"].ConnectionString;
SqlConnection con = new SqlConnection(cs);
con.Open();
SqlCommand sorgu = new SqlCommand("update tblKategori set KategoriAdi = @deger where ID = @id", con);
sorgu.Parameters.AddWithValue("@id", labelId.Text);
sorgu.Parameters.AddWithValue("@deger", txtBaslik.Text);
sorgu.ExecuteNonQuery();
// Response.Write("Kaydedildi!!!");
Page.ClientScript.RegisterStartupScript(this.GetType(), "Scripts", "<script>alert('Başlık Güncellendi!!!')</script>");
con.Close();
TreeView1.Nodes.Clear();
GetTreeViewItems();
TreeView1.ExpandAll();
temizle();
}
}