![图片](https://i-blog.csdnimg.cn/blog_migrate/f59d5fd3efc2b4ef4df86ddbd4a4b45a.gif)
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="AddKind.aspx.cs" Inherits="MyAdmin_AddKind" %>
<!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>无标题页</title>
<style type="text/css">
.style1
{
width: 800px;
}
.style2
{
width: 204px;
}
</style>
</head>
<body>
<form id="form1" runat="server">
<div>
<table cellpadding="0" cellspacing="0" class="style1">
<tr>
<td>
</td>
<td class="style2">
<asp:TreeView ID="tvKind" runat="server"
onselectednodechanged="tvKind_SelectedNodeChanged" Width="128px">
</asp:TreeView>
</td>
<td>
父节点:<asp:TextBox ID="txtParent" runat="server" Width="223px"></asp:TextBox>
<br />
<asp:Label ID="Label1" runat="server"></asp:Label>
<br />
子节点:<asp:TextBox ID="txtChild" runat="server" Width="221px"></asp:TextBox>
<br />
<br />
<asp:Button ID="btnAdd" runat="server" οnclick="btnAdd_Click" Text="添加" />
<asp:Button ID="btnUpdate" runat="server" οnclick="btnUpdate_Click" Text="修改" />
<asp:Button ID="btnDelete" runat="server" οnclick="btnDelete_Click" Text="删除" />
</td>
<td>
</td>
</tr>
</table>
</div>
</form>
</body>
</html>
。。。。。。。。。。。。。。。。。。。。
using System;
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;
using System.Data.SqlClient;
public partial class MyAdmin_AddKind : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
TVGenNode();
}
private void TVGenNode()
{
this.tvKind.Nodes.Clear();
TreeNode tnGen = new TreeNode();
tnGen.Text = "新闻类别管理";
tnGen.Value = "F000";
tnGen.Expanded = true;
tvKind.Nodes.Add(tnGen);
TVParentNode(tnGen);
}
public void TVParentNode(TreeNode tnParent)
{
string sqlStr = System.Configuration.ConfigurationManager.ConnectionStrings["ZYNewsConnStr"].ConnectionString;
SqlConnection conn = new SqlConnection(sqlStr);
conn.Open();
SqlDataAdapter sda = new SqlDataAdapter("select * from mykind where parentid='F000'", conn);
DataSet ds = new DataSet();
sda.Fill(ds);
foreach (DataRow dr in ds.Tables[0].Rows)
{
TreeNode parentNode = new TreeNode();
parentNode.Text = (string)dr["kindName"];
parentNode.Value = (string)dr["smallId"];
parentNode.Expanded = true;
tnParent.ChildNodes.Add(parentNode);
TVChildNode(parentNode);
}
}
public void TVChildNode(TreeNode tnParent)
{
string sqlStr = System.Configuration.ConfigurationManager.ConnectionStrings["ZYNewsConnStr"].ConnectionString;
SqlConnection conn = new SqlConnection(sqlStr);
conn.Open();
SqlDataAdapter sda = new SqlDataAdapter("select * from mykind where parentid='" + tnParent.Value + "'", conn);
DataSet ds = new DataSet();
sda.Fill(ds);
foreach (DataRow dr in ds.Tables[0].Rows)
{
TreeNode parentNode = new TreeNode();
parentNode.Text = (string)dr["kindName"];
parentNode.Value = (string)dr["smallId"];
parentNode.Expanded =true;
tnParent.ChildNodes.Add(parentNode);
}
}
protected void btnDelete_Click(object sender, EventArgs e)
{
string sqlStr = System.Configuration.ConfigurationManager.ConnectionStrings["ZYNewsConnStr"].ConnectionString;
SqlConnection conn = new SqlConnection(sqlStr);
conn.Open();
SqlCommand comm = new SqlCommand("delete from mykind where smallId='"+Label1.Text+"'",conn);
comm.ExecuteNonQuery();
TVGenNode();
}
protected void btnUpdate_Click(object sender, EventArgs e)
{
string sqlStr = System.Configuration.ConfigurationManager.ConnectionStrings["ZYNewsConnStr"].ConnectionString;
SqlConnection conn = new SqlConnection(sqlStr);
conn.Open();
SqlCommand comm = new SqlCommand("update mykind set kindname='"+txtParent.Text+"' where smallId='" + Label1.Text + "'", conn);
comm.ExecuteNonQuery();
TVGenNode();
}
protected void btnAdd_Click(object sender, EventArgs e)
{
string sqlStr = System.Configuration.ConfigurationManager.ConnectionStrings["ZYNewsConnStr"].ConnectionString;
SqlConnection conn = new SqlConnection(sqlStr);
conn.Open();
SqlDataAdapter sda = new SqlDataAdapter("select smallId from mykind",conn);
DataSet ds = new DataSet();
sda.Fill(ds);
string smallID = "";
if (ds.Tables[0].Rows.Count == 0)
smallID = "F000";
else
smallID="F"+(Convert.ToInt32(ds.Tables[0].Rows[ds.Tables[0].Rows.Count-1][0].ToString().Substring(1,3))+1);
SqlCommand comm = new SqlCommand("insert into mykind(smallId,kindname,parentid) values('"+smallID+"','"+txtChild.Text+"','"+Label1.Text+"') ", conn);
comm.ExecuteNonQuery();
TVGenNode();
}
protected void tvKind_SelectedNodeChanged(object sender, EventArgs e)
{
TreeNode treenode = this.tvKind.SelectedNode;
txtParent.Text = treenode.Text;
Label1.Text = treenode.Value;
}
}