asp .Net TreeView实现数据绑定和事件响应
最近做了一个图书馆管理系统,其中要实现中图法分类号查询,因为初学asp ,感觉还有点难度,
第一步:数据库文件

第二步 向界面中拖进TreeView控件
第三步添加事件
下面是cs文件代码
//TreeView绑定数据库
public void BindDataBase(string sql){
string str = System.Configuration.ConfigurationManager.
ConnectionStrings["conn"].ToString();
SqlConnection connection = new SqlConnection(str);
try
{
connection.Open();
}
catch (Exception ex)
{
Response.Write(ex.Message);
} SqlCommand sqlCmd = new SqlCommand(sql, connection); SqlDataAdapter custDA = new SqlDataAdapter();
custDA.SelectCommand = sqlCmd;
DataSet ds = new DataSet();
custDA.Fill(ds, "BookType"); TreeNode root = new TreeNode("中图法分类");//新建根节点
this.Kind_Tree.Nodes.Add(root);
for (int i = ; i < ds.Tables["BookType"].Rows.Count; i++)
{
string parent=ds.Tables["BookType"].Rows[i][].ToString();//获取数据库中标记节点
if(parent=="root"){//判断是否是根节点下的子节点
TreeNode tree2 = new TreeNode(ds.Tables["BookType"].Rows[i][].ToString()
+ ds.Tables["BookType"].Rows[i][].ToString());//创建一级子节点
root.ChildNodes.Add(tree2);
/*for (int j = 0; j < ds.Tables["BookType"].Rows.Count; j++)
{
string myparent = ds.Tables["BookType"].Rows[i][2].ToString();
if (myparent == tree2.Text.ToString())
{
TreeNode tree3 = new TreeNode(ds.Tables["BookType"].Rows[i][0].ToString() + ds.Tables["BookType"].Rows[i][1].ToString());
tree2.ChildNodes.Add(tree3);
}
}*/
}
}
for (int j = ; j < root.ChildNodes.Count; j++)
{
TreeNode rootChild = root.ChildNodes[j];//获取根节点下的子节点
for (int k = ; k < ds.Tables["BookType"].Rows.Count; k++)
{
string parent = ds.Tables["BookType"].Rows[k][].ToString();//获取数据库中标记节点 if (parent == rootChild.Text.ToString().Substring(,))//判断是否是二级子节点
{
TreeNode tree3 = new TreeNode(ds.Tables["BookType"].Rows[k][].ToString()
+"-"+ ds.Tables["BookType"].Rows[k][].ToString());//新建二级子节点
rootChild.ChildNodes.Add(tree3);
}
}
} //TreeNode rootChil = root.ChildNodes[0];
// Response.Write(rootChil.Text.ToString().Substring(0,1));
connection.Close();
} //TreeView的响应事件 protected void Kind_Tree_SelectedNodeChanged(object sender, EventArgs e)
{
string s = Kind_Tree.SelectedNode.ValuePath.ToString();
char[] spitChar = new char[] { '/' };
string[] nodeValues = s.Split(spitChar);
string sql = null;
if (nodeValues.Length == )
{
string typeId = nodeValues[].Substring(, );
sql = "select 图书编号,出版社,作者,书名,图书ISBN号,分类 from bookTable d, BookType t where t.BoType_id like '"
+ typeId + "%' and t.BoType_name=d.分类";
}
else {
char[] spitChar2 = new char[] { '-' };
string[] typeId2 = nodeValues[].Split(spitChar2);
sql = "select 图书编号,出版社,作者,书名,图书ISBN号,分类 from bookTable d, BookType t where t.BoType_id ='"
+ typeId2[] + "' and t.BoType_name=d.分类";
} SqlDataSource1.SelectCommand = sql;
Response.Write(nodeValues.Length);
}
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack) { string sql = "select * from BookType";
BindDataBase(sql);
Kind_Tree.ShowLines = true;
Kind_Tree.ExpandDepth = ; }
}
显示时,因为节点的文本太长,如果要设置自动换行,还需要设置TreeView的属性
NodeWrap = true
asp .Net TreeView实现数据绑定和事件响应的更多相关文章
- 【转载】Asp.Net中应用程序的事件响应次序
Asp.Net应用程序事件响应次序是指Application事件的相应次序,涉及到的事件包括Application_Start事件.BeginRequest事件.AuthenticateRequest ...
- 转:asp.net TreeView CheckChanged 事件浅谈
http://blog.csdn.net/xiage/article/details/5128755 在开发中经常可以碰到类似的问题: 想通过一个树父节点的TreeNodeCheckChanged 事 ...
- ASP.NET -- WebForm -- 页面生命周期事件
ASP.NET -- WebForm -- 页面生命周期事件在页生命周期的每个阶段中,页将引发可运行您自己的代码进行处理的事件. 1. PreInit: 使用该事件来执行下列操作: 检查 IsPos ...
- .net中的TreeView的数据绑定与EasyUi_tree的数据绑定
昨天看到了.net中的TreeView,学习了一波TreeView的数据绑定,联想到EasyUi中的Tree的数据,觉得里面的逻辑差不多,就总结了一下两者的数据绑定. 前端页面和必要的JS如下 < ...
- asp.net学习之 数据绑定控件--List数据绑定控件
原文:asp.net学习之 数据绑定控件--List数据绑定控件 List控件(如 CheckBoxList.DropDownList.ListBox 和 RadioButtonList 类)继承自L ...
- asp.net学习之数据绑定控件、数据源控件概述
原文:asp.net学习之数据绑定控件.数据源控件概述 1.asp.net数据绑定控件分为三大类,每个类分别进行详细: ● 列表式数据绑定控件: 列表式数据绑定控件常用来在一个表格内的一个字 ...
- asp.net学习之 数据绑定控件--表格绑定控件
原文:asp.net学习之 数据绑定控件--表格绑定控件 数据绑定 Web 服务器控件是指可绑定到数据源控件,以实现在 Web 应用程序中轻松显示和修改数据的控件.数据绑定 Web 服务器控件 ...
- ASP.Net 自定义HttpModule注册管道事件
背景: 一个请求在到达处理器时 可能需要做很多重复的工作 比如使用固定的算法推算出用户id 角色 如果在应用程序各处做重复解析 会产生大量代码冗余 还有能想到的诸如记录访问者 应用日志 统计流量 ...
- ASP.NET机制详细的管道事件流程(转)
ASP.NET机制详细的管道事件流程 第一:浏览器向服务器发送请求. 1)浏览器向iis服务器发送请求网址的域名,根据http协议封装成请求报文,通过dns解析请求的ip地址,接着通过socket与i ...
随机推荐
- 【Leetcode】Longest Palindromic Substring
问题:https://leetcode.com/problems/longest-palindromic-substring/ 给定一个字符串 S,求出 S 的最长回文子串 思路: 1. 回文:一个字 ...
- GregorianCalendar类
Calendar类实现了公历日历,GregorianCalendar是Calendar类的一个具体实现. Calendar 的getInstance()方法返回一个默认用当前的语言环境和时区初始化的G ...
- javascript平时小例子⑦(鼠标跟随的div)
<!doctype html><html> <head> <meta charset="utf-8"> <title>无 ...
- 关于各种类型数据char、int、double、float 所占空间长度的计算,而char类型让我长姿势了
#include <iostream> int main() { using namespace std; //int A=10; //double B=6; cout << ...
- SQL SERVER 合并重复行,行列转换
引用自:http://www.cnblogs.com/love-summer/archive/2012/03/27/2419778.html sql server2000 里面如何实现oracle10 ...
- java内置数据类型
常量在程序运行时,不会被修改的量. 在 Java 中使用 final 关键字来修饰常量,声明方式和变量类似: finaldouble PI =3.1415927; 虽然常量名也可以用小写,但为了便于识 ...
- 在Windows下使用Git
关于Git git是当今最流行的版本控制系统,因为是诞生在Linux操作系统下,因此Linux对git天生有最好的支持,但好在各路大牛的努力下,目前在Windows也能较为完美的使用.以下便是我使用g ...
- php 获取中文长度 截取中文字符串
#获取中文长度mb_strlen($str,$encoding); #截取中文字符串 mb_substr(str,start,length,encoding);
- 基于FormsAuthentication的用户、角色身份认证
一般情况下,在我们做访问权限管理的时候,会把用户的正确登录后的基本信息保存在Session中,以后用户每次请求页面或接口数据的时候,拿到 Session中存储的用户基本信息,查看比较他有没有登录和能否 ...
- laravel5 使用model 表名总是多个s
正常,如果不要s,请在model 指定表名. class user extend Model{ public $table='user';//这样寻找的就是没s的表 }