asp.net treeview数据库绑定 (节点添加 删除 修改)

来源:互联网 发布:c语言flag是什么意思 编辑:程序博客网 时间:2024/05/16 01:09

 
图片

<%@ 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>
                    &nbsp;</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 />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                    <asp:Button ID="btnAdd" runat="server" onclick="btnAdd_Click" Text="添加" />
&nbsp;&nbsp;&nbsp;
                    <asp:Button ID="btnUpdate" runat="server" onclick="btnUpdate_Click" Text="修改" />
&nbsp;&nbsp;&nbsp;&nbsp;
                    <asp:Button ID="btnDelete" runat="server" onclick="btnDelete_Click" Text="删除" />
                </td>
                <td>
                    &nbsp;</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;
    }
}
 

原创粉丝点击