Sınırsız Kategori Mantığı .Net

DB TABLO SCRİPTİ :

CREATE TABLE TBL_KATEGORILER
(
ID INT IDENTITY(1,1) NOT NULL,

KAT_ID INT NULL,

KAT_ISMI NVARCHAR(50) NULL
)

HTML KODLARI :

<%@ 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>Sınırsız Kategori | ul, li</title>

</head>

<body>

<form id=”form1″ runat=”server”>

<div>

<ul id=”Menu1″ runat=”server”></ul>

</div>

</form>

</body>

</html>

C# KODLARI:

using System.Collections.Generic;

using System.Web;

using System.Web.UI;

using System.Web.UI.WebControls;

using System.Data;

using System.Data.SqlClient;

using System.Web.UI.HtmlControls;

using System.Configuration;

public partial class _Default : System.Web.UI.Page
{
DataSet ds = new DataSet();

protected void Page_Load(object sender, EventArgs e)
{
doldur();
}

private void doldur()
{
SqlDataAdapter da = new SqlDataAdapter(“SELECT * FROM TBL_KATEGORILER”, ConfigurationManager.ConnectionStrings[“Baglan”].ConnectionString);

da.Fill(ds);

DataRow[] anaKategoriler = ds.Tables[0].Select(“KAT_ID = 0”);

foreach (DataRow anaKategori in anaKategoriler)
{

HtmlGenericControl li = new HtmlGenericControl(“li”);

li.Controls.Add(new LiteralControl(“<a href='<%#Eval(‘ID’)%>’>” + anaKategori[“KAT_ISMI”].ToString() + “</a>”));

Menu1.Controls.Add(li);

altmenu(Convert.ToInt32(anaKategori[“ID”]));

}
}

private void altmenu(int kategoriId)
{

DataRow[] kategoriler = ds.Tables[0].Select(“KAT_ID=” + kategoriId);

if (kategoriler.Length == 0)
{

}
else
{

Menu1.Controls.Add(new LiteralControl(“<ul>”));

foreach (DataRow kategori in kategoriler)
{

HtmlGenericControl li1 = new HtmlGenericControl(“li”);

li1.Controls.Add(new LiteralControl(“<a href=’#’>” + kategori[“KAT_ISMI”].ToString() + “</a>”));

Menu1.Controls.Add(li1);

altmenu(Convert.ToInt32(kategori[“ID”]));
}

Menu1.Controls.Add(new LiteralControl(“</ul>”));
}
}
}