asp.net:AJAX+LINQ+TreeView动态填充多级节点



演示示例为一个学生信息查看菜单:先选择部门,再选择班级,最后选择学生姓名,查看学生信息;



效果图:

  效果图

 

采用TreeView的SelectedNodeChanged事件作为一个包含用来显示学生信息的Lable的UpdatePanel的触发器,如下:



<form id=”form1” runat=”server”>
<div>
<asp:ScriptManager ID=”ScriptManager1” runat=”server”>
</asp:ScriptManager>
<asp:UpdatePanel ID=”UpdatePanel1” runat=”server” UpdateMode=”Conditional”>
<ContentTemplate>
<asp:Label ID=”Label1” runat=”server” Text=”Label”></asp:Label>
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID=”TreeView1”
EventName=”SelectedNodeChanged” />
</Triggers>
</asp:UpdatePanel>
<asp:TreeView ID=”TreeView1” runat=”server”
onselectednodechanged=”TreeView1_SelectedNodeChanged”
ontreenodepopulate=”TreeView1_TreeNodePopulate” Width=”169px”
ExpandDepth=”0” ShowLines=”True”>
<Nodes>
<asp:TreeNode Text=”部门->班级->学生” SelectAction=”Expand” PopulateOnDemand=”true” Value=”请选择”></asp:TreeNode>
</Nodes>
</asp:TreeView>

</div>
</form>


 

其中一些涉及数据库的查询,采用LINQ的写法,再此不多作说明,其先决条件:在“服务器资源管理器”中添加连接,选择数据库,右击“解决方案资源管理器”,选择“添加新项”,选择“LINQ to sql类”,将你需要用的表从“服务器资源管理器”拖入跳出的界面中,一定要保存,好了,完整代码如下:


using System;
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.Text;//StringBuilder所在命名空间

public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{

}

protected void TreeView1_SelectedNodeChanged(object sender, EventArgs e)
{
JWInfoDataContext db = new JWInfoDataContext();

//LNNQ写法
var result = (from stuIn in db.学生信息
where stuIn.学号.ToString() == this.TreeView1.SelectedNode.Value.ToString()
select stuIn).First();
if (result != null)
{
StringBuilder strStuInfo = new StringBuilder();
strStuInfo.AppendLine(“学号:” + result.学号.ToString());
strStuInfo.AppendLine(“<br />姓名:” + result.姓名.ToString());
strStuInfo.AppendLine(“<br />性别:” + result.性别.ToString());
strStuInfo.AppendLine(“<br />班级编号:” + result.班级编号.ToString());
Label1.Text = strStuInfo.ToString();
}
else
{
Label1.Text = “没有数据!”;
}
}

protected void TreeView1_TreeNodePopulate(object sender, TreeNodeEventArgs e)
{
//获取根节点深度,*注意设置TreeView的ExpandDepth=”0”,即默认根节点深度为0
switch (e.Node.Depth)
{
case 0://填充一级节点
PopulateDept(e.Node);
break;
case 1://填充二级节点
PopulateClassID(e.Node);
break;
case 2://三级
PopulateStu(e.Node);
break;
default:
break;
}
}

/// <summary>
/// 填充一级节点的代码
/// </summary>
/// <param name=”node”></param>
private void PopulateDept(TreeNode node)
{
DataTable dt = RunQuery(“PopulateDept”, null);

if (dt.Rows.Count > 0)
{
foreach (DataRow row in dt.Rows)
{
//创建一级节点
TreeNode newNode = new TreeNode();
newNode.Text = row[“部门名称”].ToString();
newNode.Value = row[“部门编号”].ToString();

//指示为动态填充节点
newNode.PopulateOnDemand = true;

//设置选择节点时激发TreeNodeExpanded 事件
newNode.SelectAction = TreeNodeSelectAction.Expand;

//将子节点加入父节点中
node.ChildNodes.Add(newNode);
}
}
}

/// <summary>
/// 填充二级节点
/// </summary>
/// <param name=”node”></param>
private void PopulateClassID(TreeNode node)
{
DataTable dt = RunQuery(“PopulateClassID”, node.Value);

if (dt.Rows.Count > 0)
{
foreach (DataRow row in dt.Rows)
{
TreeNode newNode = new TreeNode();
newNode.Text = row[“班级名称”].ToString();
newNode.Value = row[“班级编号”].ToString();

newNode.PopulateOnDemand = true;

newNode.SelectAction = TreeNodeSelectAction.Expand;

node.ChildNodes.Add(newNode);
}
}
}

/// <summary>
/// 填充末节点(学生列表)
/// </summary>
/// <param name=”node”></param>
private void PopulateStu(TreeNode node)
{
DataTable dt = RunQuery(“PopulateStu”, node.Value);

if (dt.Rows.Count > 0)
{
foreach (DataRow row in dt.Rows)
{
TreeNode newNode = new TreeNode();
newNode.Text = row[“姓名”].ToString();
newNode.Value = row[“学号”].ToString();

//不设置动态填充节点
newNode.PopulateOnDemand = false;

//设置选择该级节点时引发 SelectedNodeChanged 事件
newNode.SelectAction = TreeNodeSelectAction.Select;

node.ChildNodes.Add(newNode);
}
}
}

/// <summary>
/// 取得节点信息
/// </summary>
/// <param name=”QueryString”>填充节点等级说明</param>
/// <param name=”nodeValue”>筛选条件</param>
/// <returns>查询结果</returns>
private DataTable RunQuery(string QueryString, string nodeValue)
{
DataTable mytable = new DataTable();

if (QueryString == “PopulateDept”)
{
mytable.Columns.Add(“部门编号”, typeof(string));
mytable.Columns.Add(“部门名称”, typeof(string));

//LINQ实体操作类,生成的
JWInfoDataContext db = new JWInfoDataContext();

var result = from dept in db.部门
select new { dept.部门编号, dept.部门名称 };

foreach (var item in result)
{
DataRow newRow = mytable.NewRow();
newRow[“部门编号”] = item.部门编号.ToString();
newRow[“部门名称”] = item.部门名称.ToString();
mytable.Rows.Add(newRow);
}
}
else if (QueryString == “PopulateClassID”)
{
mytable.Columns.Add(“班级编号”, typeof(string));
mytable.Columns.Add(“班级名称”, typeof(string));

JWInfoDataContext db = new JWInfoDataContext();
var reuslt = from Class in db.班级
where Class.部门编号 == nodeValue
select new { Class.班级编号, Class.班级名称 };

foreach (var item in reuslt)
{
DataRow row = mytable.NewRow();
row[“班级编号”] = item.班级编号.ToString();
row[“班级名称”] = item.班级名称.ToString();
mytable.Rows.Add(row);
}
}

else if (QueryString == “PopulateStu”)
{
mytable.Columns.Add(“学号”, typeof(string));
mytable.Columns.Add(“姓名”, typeof(string));

JWInfoDataContext db = new JWInfoDataContext();

var result = from stu in db.学生信息
where stu.班级编号 == nodeValue
select new { stu.学号, stu.姓名 };

foreach (var item in result)
{
DataRow row = mytable.NewRow();
row[“学号”] = item.学号.ToString();
row[“姓名”] = item.姓名.ToString();
mytable.Rows.Add(row);
}
}
return mytable;
}
}


 

 

 

 

 



版权声明:本文为博主原创文章,未经博主允许不得转载。