示範示例為一個學生資訊檢視菜單:先選擇部門,再選擇班級,最後選擇學生姓名,檢視學生資訊;
效果圖:
采用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);
case 2://三級
PopulateStu(e.Node);
default:
/// <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);
}
/// 填充二級節點
private void PopulateClassID(TreeNode node)
DataTable dt = RunQuery("PopulateClassID", node.Value);
newNode.Text = row["班級名稱"].ToString();
newNode.Value = row["班級編号"].ToString();
/// 填充末節點(學生清單)
private void PopulateStu(TreeNode node)
DataTable dt = RunQuery("PopulateStu", node.Value);
newNode.Text = row["姓名"].ToString();
newNode.Value = row["學号"].ToString();
//不設定動态填充節點
newNode.PopulateOnDemand = false;
//設定選擇該級節點時引發 SelectedNodeChanged 事件
newNode.SelectAction = TreeNodeSelectAction.Select;
/// 取得節點資訊
/// <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));
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));
var result = from stu in db.學生資訊
where stu.班級編号 == nodeValue
select new { stu.學号, stu.姓名 };
row["學号"] = item.學号.ToString();
row["姓名"] = item.姓名.ToString();
return mytable;
}
原文釋出時間為:2010-01-18
本文作者:vinoYang