当前位置:编程学习 > C#/ASP.NET >>

C#中如何判断TreeView选中

当右击TreeView时,如何判断TreeView中有没有选中项

请高手解答,谢谢!

答案:使用TreeView的AfterSelect事件

private void treeView1_AfterSelect(object sender, TreeViewEventArgs e)

{

       MessageBox.Show(e.Node.Text);//举例:弹出对话框,显示结点文本

}

 

 

if (treeView1.SelectedNode == null)//Treeview没有被选中结点,可以添加到Form_Load事件中测试
            {                                             //当窗体加载时,显然没有选中TreeView,将会弹出下面的对话框
                MessageBox.Show("HH");
            }

.sql:

SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[AutoTree](
 [id] [int] IDENTITY(1,1) NOT NULL,
 [title] [nvarchar](50) COLLATE Chinese_PRC_CI_AS NOT NULL,
 [parent_Id] [int] NOT NULL,
 [NavigateUrl] [nvarchar](100) COLLATE Chinese_PRC_CI_AS NULL,
 CONSTRAINT [PK_Tb_Ex06_05] PRIMARY KEY CLUSTERED
(
 [id] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]

GO

TRUNCATE TABLE dbo.[AutoTree]

insert into [dbo].[AutoTree]([title],[parent_Id],[NavigateUrl])values('OA办公系统',0,'~/test.aspx')
insert into [dbo].[AutoTree]([title],[parent_Id],[NavigateUrl])values('信息中心',1,'~/test.aspx')
insert into [dbo].[AutoTree]([title],[parent_Id],[NavigateUrl])values('人员去向',1,'~/test.aspx')
insert into [dbo].[AutoTree]([title],[parent_Id],[NavigateUrl])values('工作流',1,'~/test.aspx')
insert into [dbo].[AutoTree]([title],[parent_Id],[NavigateUrl])values('名片夹',1,'~/test.aspx')
insert into [dbo].[AutoTree]([title],[parent_Id],[NavigateUrl])values('人事管理',1,'~/test.aspx')
insert into [dbo].[AutoTree]([title],[parent_Id],[NavigateUrl])values('资源预定',1,'~/test.aspx')
insert into [dbo].[AutoTree]([title],[parent_Id],[NavigateUrl])values('我的消息',2,'~/test.aspx')
insert into [dbo].[AutoTree]([title],[parent_Id],[NavigateUrl])values('公共消息',2,'~/test.aspx')
insert into [dbo].[AutoTree]([title],[parent_Id],[NavigateUrl])values('最新的申请',4,'~/test.aspx')
insert into [dbo].[AutoTree]([title],[parent_Id],[NavigateUrl])values('发送的申请',4,'~/test.aspx')
insert into [dbo].[AutoTree]([title],[parent_Id],[NavigateUrl])values('接收的申请',4,'~/test.aspx')
insert into [dbo].[AutoTree]([title],[parent_Id],[NavigateUrl])values('审批',4,'~/test.aspx')
insert into [dbo].[AutoTree]([title],[parent_Id],[NavigateUrl])values('历史记录',4,'~/test.aspx')
insert into [dbo].[AutoTree]([title],[parent_Id],[NavigateUrl])values('请假条的申请',10,'~/test.aspx')
insert into [dbo].[AutoTree]([title],[parent_Id],[NavigateUrl])values('工作总结',10,'~/test.aspx')
insert into [dbo].[AutoTree]([title],[parent_Id],[NavigateUrl])values('采购的申请',10,'~/test.aspx')
insert into [dbo].[AutoTree]([title],[parent_Id],[NavigateUrl])values('私有名片夹',5,'~/test.aspx')
insert into [dbo].[AutoTree]([title],[parent_Id],[NavigateUrl])values('公有名片夹',5,'~/test.aspx')
insert into [dbo].[AutoTree]([title],[parent_Id],[NavigateUrl])values('档案管理',6,'~/test.aspx')
insert into [dbo].[AutoTree]([title],[parent_Id],[NavigateUrl])values('职工信息管理',6,'~/test.aspx')
GO

 

.aspx:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="TreeShowPage.aspx.cs" Inherits="TreeShowPage" %>

<!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>
</head>
<body bgcolor ="#ffffcc">
    <form id="form1" runat="server">
    <div>
        <asp:TreeView ID="TreeView1" runat="server" OnSelectedNodeChanged="TreeView1_SelectedNodeChanged" ImageSet="BulletedList3" ExpandDepth="1" ExpandImageUrl="~/images/1.gif" NodeWrap="True" NoExpandImageUrl="~/images/1.gif" ShowLines="True" CollapseImageUrl="~/images/1.gif" >
            <ParentNodeStyle Font-Bold="False" />
            <HoverNodeStyle Font-Underline="True" ForeColor="#5555DD" />
            <SelectedNodeStyle Font-Underline="True" HorizontalPadding="0px"
                VerticalPadding="0px" ForeColor="#5555DD" ImageUrl="~/images/1.gif" />
            <NodeStyle Font-Names="Verdana" Font-Size="8pt" ForeColor="Black" HorizontalPadding="5px"
                NodeSpacing="0px" VerticalPadding="0px" />
            <LeafNodeStyle ImageUrl="~/images/1.gif" />
        </asp:TreeView>
   
    </div>
    </form>
</body>
</html>

 


.cs :

 

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;

public partial class TreeShowPage : System.Web.UI.Page
{
    protected SqlConnection myConn;
    protected SqlDataAdapter myAdapter;
    protected DataSet data;
    protected string query;

   
    protected void

上一个:C# 如何打开默认浏览器主页
下一个:java和c#学哪个好些?

CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,