如下 类似treeview似的。用table显示也行
数据库结构
一级目录表first
id
firstName
二级second
id
secondName
firstID 主外键一级目录1
二级目录1
二级目录2
二级目录3
一级目录2
二级目录1
。。 可以直接在页面进行增删改 并且查询展示出来如上面一样的格式
数据库结构
一级目录表first
id
firstName
二级second
id
secondName
firstID 主外键一级目录1
二级目录1
二级目录2
二级目录3
一级目录2
二级目录1
。。 可以直接在页面进行增删改 并且查询展示出来如上面一样的格式
谢谢
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
using System.Text;
using System.Collections;public partial class Default2 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
bindtree("0"); //传入第0个pid开始遍历根节点
}
}
private DataSet Getdata(string pid) //在这里我们传入一个pid
{
try
{
SqlConnection con = new SqlConnection("server=.;database=demo;uid=sa;pwd=;");
SqlCommand com = new SqlCommand(" select * from T_lei where fuleiid=" + pid, con);
SqlDataAdapter da = new SqlDataAdapter(com);
DataSet ds = new DataSet();
da.Fill(ds);
return ds; //返回含有pid的数据
}
catch (Exception ex)
{
Response.Write(ex.Message);
throw;
}
finally
{ }
}
private void bindtree(string pid)
{
DataSet ds = Getdata(pid);
if (ds.Tables[0].Rows.Count > 0)
{
for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
{
TreeNode node = new TreeNode(ds.Tables[0].Rows[i]["leiname"].ToString(), ds.Tables[0].Rows[i]["id"].ToString()); //这是在找数据库中的节点
node.NavigateUrl = ds.Tables[0].Rows[i]["url"].ToString();
this.TreeView1.Nodes.Add(node); //把这个节点添加到控件中
bindnode(node); }
}
}
private void bindnode(TreeNode nd) //就是一个递归的开始 遍历根节点下面的子节点
{
DataSet ds = Getdata(nd.Value);
for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
{
TreeNode node = new TreeNode();
node.Text = ds.Tables[0].Rows[i]["leiname"].ToString();
node.Value = ds.Tables[0].Rows[i]["id"].ToString();
node.NavigateUrl = ds.Tables[0].Rows[i]["url"].ToString();
nd.ChildNodes.Add(node);
bindnode(node);
}
} //选中节点事件
protected void TreeView1_SelectedNodeChanged(object sender, EventArgs e)
{
int leiid = int.Parse(TreeView1.SelectedNode.Value);
SqlConnection con = null;
try
{
con = new SqlConnection("server=.;database=demo;uid=sa;pwd=;");
//SqlCommand com = new SqlCommand(" select * from T_goods where leiid=" + leiid, con);
SqlDataAdapter sqlda = new SqlDataAdapter(" select * from T_lei where id=" + leiid, con);
DataSet ds = new DataSet();
sqlda.Fill(ds);
DataTable table = ds.Tables[0]; foreach (DataRow row in table.Rows)
{
Leibie leibie = new Leibie();
leibie.Id = int.Parse(row["id"].ToString());
leibie.Name = row["leiname"].ToString();
leibie.Fuleiid = int.Parse(row["fuleiid"].ToString()); Label1.Text = leibie.Id.ToString();
TextBox1.Text = leibie.Name.ToString();
TextBox3.Text = leibie.Name.ToString();
}
}
catch (Exception ex)
{
Response.Write(ex.Message);
throw;
}
finally
{
con.Close();
}
}
protected void Button1_Click(object sender, EventArgs e)
{
//修改节点
if (TreeView1.SelectedNode == null)
{
Response.Write("<script>alert('请选择节点');</script>");
}
else
{
string name = TextBox1.Text.ToString().Trim();
int id = int.Parse(Label1.Text.ToString().Trim());
try
{
SqlConnection con = new SqlConnection("server=.;database=demo;uid=sa;pwd=;");
con.Open();
SqlCommand com = new SqlCommand(" update T_lei set leiname='" + name + "'where id=" + id, con);
int i = com.ExecuteNonQuery();
if (i > 0)
{
Response.Write("<script>alert('success');</script>");
Response.Redirect("Default2.aspx");
}
}
catch (Exception)
{ throw;
}
}
}
protected void Button2_Click(object sender, EventArgs e)
{
//添加节点
string name = TextBox2.Text.ToString().Trim();
if (TreeView1.SelectedNode == null)
{
Response.Write("<script>alert('请选择父节点');</script>");
}
else
{
try
{
SqlConnection con = new SqlConnection("server=.;database=demo;uid=sa;pwd=;");
con.Open();
SqlCommand com = new SqlCommand(" insert into T_lei (leiname,fuleiid)values('" + name + "'," + int.Parse(TreeView1.SelectedNode.Value.ToString()) + ")", con);
int i = com.ExecuteNonQuery();
if (i > 0)
{
Response.Write("<script>alert('success');</script>");
Response.Redirect("Default2.aspx");
}
}
catch (Exception)
{ throw;
}
}
}
protected void Button3_Click(object sender, EventArgs e)
{
//删除节点
if (TreeView1.SelectedNode == null)
{
Response.Write("<script>alert('请选择要删除的节点');</script>");
}
else
{
//Button3.Text = TreeView1.SelectedNode.Text.ToString();
try
{
SqlConnection con = new SqlConnection("server=.;database=demo;uid=sa;pwd=;");
con.Open();
SqlCommand com = new SqlCommand(" delete from T_lei where id=" + int.Parse(TreeView1.SelectedNode.Value.ToString()), con);
int i = com.ExecuteNonQuery();
if (i > 0)
{
Response.Write("<script>alert('success');</script>");
Response.Redirect("Default2.aspx");
}
}
catch (Exception)
{ throw;
}
}
}
}