asp.net treeview数据库绑定 (节点添加 删除 修改)
2010-10-09 16:28
691 查看
[align=center][/align] <%@ 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" onclick="btnAdd_Click" Text="添加" /> <asp:Button ID="btnUpdate" runat="server" onclick="btnUpdate_Click" Text="修改" /> <asp:Button ID="btnDelete" runat="server" onclick="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.Co 4000 nfigurationManager.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; } } |
相关文章推荐
- asp.net treeview数据库绑定 (节点添加 删除 修改)
- ASP.net 中关于控件Treeview的操作------ 节点查找、删除和添加
- ASP.NET TreeView Web Control 添加,删除,修改
- asp.net treeview绑定数据库,并取节点和节点的内容
- ASP.NET(C#)对XML文件节点的添加删除修改示例
- ASP.NET - 如何:添加或删除 TreeView 节点元素
- treeview的节点从数据库中读取,怎样修改、添加、删除treeview节点
- asp.net对xml文件的读写,添加,修改,删除操作
- asp.net 操作 xml格式网站地图 添加删除修改!(简单版)
- 浅析动态添加、删除、修改TreeView节点
- ASP.NET中数据库的操作初步----增加、删除、修改
- asp.net连接数据库 增加,修改,删除,查询代码
- asp.net 对xml文件的读写,添加,修改,删除操作
- asp.net 对xml文件的读写,添加,修改,删除操作
- TreeView树型菜单添加,修改,删除,绑定DropDownList功能
- Asp.net中C#增加删除查找与修改xml文件元素节点
- asp.net 对xml文件的读写,添加,修改,删除操作
- 关于C#和ASP.NET中对App.config和Web.config文件里的[appSettings]和[connectionStrings]节点进行新增、修改、删除和读取相关的操作
- asp.net TreeView动态绑定数据库显示数据
- 【gtk#】关于treeview数据绑定,选择及添加删除节点数据的备忘录