asp.net中使用TreeView控件

本文介绍了一个使用ASP.NET和SQL Server实现的动态菜单树导航系统。通过数据库存储菜单结构,并利用TreeView控件动态生成菜单,实现了良好的用户体验。文章详细展示了SQL脚本创建表结构、插入数据的过程以及前后端代码实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 

 

//sql脚本

CREATE TABLE [sysMenuTree] (
[NoteId] [decimal]  (18,0) NOT NULL,
[ParentId] [decimal]  (18,0) NULL,
[sText] [nvarchar]  (50) NULL,
[sValue] [nvarchar]  (50) NULL,
[sURL] [nvarchar]  (50) NULL,
[sTarget] [nvarchar]  (50) NULL,
[Chger] [nvarchar]  (50) NULL,
[ChgTime] [nvarchar]  (50) NULL)

INSERT [sysMenuTree] ([NoteId],[ParentId],[sText],[sValue]) VALUES ( 3,0,N'目錄1',N'目錄10')
INSERT [sysMenuTree] ([NoteId],[ParentId],[sText],[sValue]) VALUES ( 4,0,N'目錄2',N'目錄11')
INSERT [sysMenuTree] ([NoteId],[ParentId],[sText],[sValue]) VALUES ( 5,0,N'目錄3',N'目錄12')
INSERT [sysMenuTree] ([NoteId],[ParentId],[sText],[sValue]) VALUES ( 6,3,N'項目.1',N'項目.1')
INSERT [sysMenuTree] ([NoteId],[ParentId],[sText],[sValue]) VALUES ( 7,3,N'項目.2',N'項目.2')
INSERT [sysMenuTree] ([NoteId],[ParentId],[sText],[sValue]) VALUES ( 8,4,N'項目.1',N'項目.1')
INSERT [sysMenuTree] ([NoteId],[ParentId],[sText],[sValue]) VALUES ( 9,4,N'項目.2',N'項目.2')
INSERT [sysMenuTree] ([NoteId],[ParentId],[sText],[sValue]) VALUES ( 10,4,N'項目.3',N'項目.3')
INSERT [sysMenuTree] ([NoteId],[ParentId],[sText],[sValue]) VALUES ( 11,5,N'項目.1',N'項目.1')
INSERT [sysMenuTree] ([NoteId],[ParentId],[sText],[sValue]) VALUES ( 12,5,N'項目.2',N'項目.2')

 

 

 

 

 

 

//前台

 

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>

 

 

<form id="form1" runat="server">
   

    <div style=" background-image:url(/images/山水图画.jpg); width:40%; height:30px;" >
   
   
    </div>


    <table style="width:100%;">
   
        <tr>
           
            <td style=" background-image:url(/images/山水图画.jpg); width:40%; height:30px;">
            </td>
   
            <td>
                &nbsp; &nbsp;<asp:LinkButton Text="首页" ID="labHome" Font-Size="XX-Large"
                    runat="server" CausesValidation="false" οnclick="labHome_Click"></asp:LinkButton>
            </td>
            <td style="width: 10px; font-weight: bold;">
                |
            </td>
            <td>
                <asp:LinkButton Text="个人信息" ID="labPersonalInfomation" runat="server"
                    CausesValidation="false" οnclick="labPersonalInfomation_Click"></asp:LinkButton>
            </td>

            <td style="width: 10px; font-weight: bold;">
                |
            </td>
           
            <td>
                <asp:LinkButton Text="健康状况" ID="LabHealthInfomation" runat="server"
                    CausesValidation="false" οnclick="LabHealthInfomation_Click"></asp:LinkButton>
            </td>
            <td style="width: 10px; font-weight: bold;">
                |
            </td>
            <td>
                <asp:LinkButton Text="检查记录" ID="LabConsultationRecord" runat="server"
                    CausesValidation="false" οnclick="LabConsultationRecord_Click"></asp:LinkButton>
            </td>
            <td style="width: 10px; font-weight: bold;">
                |
            </td>
            <td>
                <asp:LinkButton Text="理疗化验" ID="LabPhysicalAndTest" runat="server"
                    CausesValidation="false" οnclick="LabPhysicalAndTest_Click"></asp:LinkButton>
            </td>
            <td style="width: 10px; font-weight: bold;">
                |
            </td>
            <td>
                <asp:LinkButton Text="活动中心" ID="LabHealthyService" runat="server"
                    CausesValidation="false" οnclick="LabHealthyService_Click"></asp:LinkButton>
            </td>
            <td style="width: 10px; font-weight: bold;">
                |
            </td>
            <td>
                <asp:LinkButton Text="康复指导" ID="LabGuideToHealth" runat="server"
                    CausesValidation="false" οnclick="LabGuideToHealth_Click"></asp:LinkButton>
            </td>
            <td style="width: 10px; font-weight: bold;">
                |
            </td>
            <td>
                <asp:LinkButton Text="咨询中心" ID="LabHealthInquiry" runat="server"
                    CausesValidation="false" οnclick="LabHealthInquiry_Click"></asp:LinkButton>
                &nbsp;
            </td>

           <td style="width: 10px; font-weight: bold;">
                |
            </td>


            <td>
                <asp:LinkButton OnClientClick="return confirm('确认是否退出!')" Text="安全退出" ID="eixts"
                    runat="server" CausesValidation="false" OnClick="eixts_Click"></asp:LinkButton>
                   
                  

                    &nbsp;
                    &nbsp;

            </td>

        </tr>
       
    </table>

 

   
    <div style="width:100%; height:450px;">
   
    <div style=" width:15%; height:100%; background: #6cf; float:left;">

         <asp:TreeView ID="treeMenu" runat="server"
             onselectednodechanged="treeMenu_SelectedNodeChanged">
        </asp:TreeView>

    </div>   

    <div style="width:85%;background: #cff; height:100%; float:right;">
   
        <iframe id="myFrame" allowTransparency="true" style=" background-color:#cff;"
            runat="server" src="IframeWebForm.aspx" width="100%;" height="85%;" name="I1"></iframe>
   

     <%--<iframe id="Iframe1" runat="server" src="IframeWebForm.aspx" width="100%;" height="100%;"></iframe>--%>

 

    </div>

 

   </div>

 

     <div>
        <center>
            <table>
                <tr>
                    <td>
                        <table style="width: 100%;">
                            <tr>
                                <td style="text-align: center; color: #428260; font-size: 14px; font-family: 黑体;">
                                    &copy;版权所有&nbsp;&nbsp;我的测试&nbsp;&nbsp;&nbsp;&nbsp;电话:0500-33856565
                                </td>
                            </tr>
                            <tr>
                                <td style="text-align: center; font-family: 'Times New Roman', Times, serif; font-size: 12px;
                                    color: #669999;">
                                    技术支持:&nbsp;某某科技股份有限公司&nbsp;&nbsp;&nbsp;信息中心日常维护
                                </td>
                            </tr>
                        </table>
                    </td>
                </tr>
            </table>
        </center>
    </div>

    </form>

 

 

 

</body>
</html>

 

 

//后台代码

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;

namespace WebApplicationUI
{
    public partial class 使用TreeView实现动态页面导航 : System.Web.UI.Page
    {

        private readonly string ConnString = @"server=myserver;
database=JoinTable;uid=sa;pwd=111";

        private DataTable dt = null;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                dt = new DataTable();
                GetMenuToDataTable("select * from sysMenuTree", dt);

                BindTree(dt, null, "0");
            }
        }

        //绑定TreeView
        private void BindTree(DataTable dtSource, TreeNode parentNode, string parentID)
        {
            DataRow[] rows = dtSource.Select(string.Format("ParentID={0}", parentID));
            foreach (DataRow row in rows)
            {
                TreeNode node = new TreeNode();
                node.Text = row["sText"].ToString();
                node.Value = row["sValue"].ToString();
                BindTree(dtSource, node, row["NoteId"].ToString());
                if (parentNode == null)
                {
                    treeMenu.Nodes.Add(node);
                }
                else
                {
                    parentNode.ChildNodes.Add(node);
                }
            }
        }

        //返回按钮的数据集DataTable
        private DataTable GetMenuToDataTable(string query, DataTable dt)
        {
            using (SqlConnection conn = new SqlConnection(ConnString))
            {
                SqlCommand cmd = new SqlCommand(query, conn);
                SqlDataAdapter ada = new SqlDataAdapter(cmd);
                ada.Fill(dt);
            }
            return dt;
        }

 


        protected void treeMenu_SelectedNodeChanged(object sender, EventArgs e)
        {
            //测试选中节点的深度
            //Response.Write(treeMenu.SelectedNode.Depth+" 深度");


            //如果选中的深度不是0,即选中的不是父节点的时候才打印文本值和value
            if (treeMenu.SelectedNode.Depth!=0)
            {
                //Response.Write(treeMenu.SelectedNode.Text + "  (文本)");
                //Response.Write(treeMenu.SelectedNode.Value + "  (值)");

           

                //实现Treeiew页面导航的效果思路就是:
                //判断选中的TreeView节点的内容,然后根据得到的节点内容来进行页面内容的跳转,例如

 

             // if(treeMenu.SelectedNode.Text=="test")

             //{

              //    myFrame.Attributes["src"] = "/IframeWebForm.aspx";

          //  }

          

                //Response.Redirect("~/IframeWebForm.aspx?name="+treeMenu.SelectedNode.Text);

                myFrame.Attributes["src"] = "/IframeWebForm.aspx?name=" + treeMenu.SelectedNode.Text;
            }

          
        }

    }
}

 

 

//Iframe前台页面

 

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
   

       文本 <asp:TextBox ID="txtGetValue" runat="server"></asp:TextBox> &nbsp &nbsp

        <asp:Button ID="btnSubmit" runat="server" Text="按钮" />

    </div>
    </form>
</body>
</html>

 

 

//Iframe后台页面

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace WebApplicationUI
{
    public partial class IframeWebForm : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            string name = Request["name"];
            txtGetValue.Text = name;


        }
    }
}

 

 

 

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值