C#中如何判断TreeView选中

当右击TreeView时,如何判断TreeView中有没有选中项请高手解答,谢谢!... 当右击TreeView时,如何判断TreeView中有没有选中项请高手解答,谢谢! 展开
 我来答
匿名用户
2013-08-16
展开全部
.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]GOTRUNCATE 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 Page_Load(object sender, EventArgs e)
{ if (!IsPostBack)
{
//获取数据连接语句,并创建数据库连接对象
myConn = GetConnection();
CreateDataSet();
InitTree(TreeView1.Nodes, "0");
}
}
public DataSet CreateDataSet()
{
query = "select * from [AutoTree]";
myAdapter = new SqlDataAdapter(query, myConn);
data = new DataSet();
myAdapter.Fill(data, "tree");
return data; }
//从DataSet中取数据建树
//从根节点开始递归调用显示子树
public void InitTree(TreeNodeCollection Nds, string parentId)
{
TreeNode NewNode;
//data为存储建树数据信息的数据集
//用父节点进行筛选数据集中信息
DataRow[] rows = data.Tables[0].Select("parent_Id='" + parentId + "'");
foreach (DataRow row in rows)
{ NewNode = new
TreeNode(row["title"].ToString(),
row["id"].ToString());
Nds.Add(NewNode);

InitTree(NewNode.ChildNodes, row["id"].ToString());
}
} protected void TreeView1_SelectedNodeChanged(object sender, EventArgs e)
{ int nodeId = Convert.ToInt32(TreeView1.SelectedValue);
string url = GetUrl(nodeId);
Response.Redirect(url.ToString()); } public string GetUrl(int filesId)
{
//获得url地址
string url ="";
return url;
} public SqlConnection GetConnection()
{
//获取数据连接语句,并创建数据库连接对象
String conn = ConfigurationManager.AppSettings["ConnectionString"].ToString();
SqlConnection myConn;
myConn = new SqlConnection(conn);
return myConn;
}
}
匿名用户
2013-08-16
展开全部
使用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");
}
本回答被网友采纳
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式