数据动态从数据库加载
using SystemDataOleDb;
public partial class Default : SystemWebUIPage
{
protected void Page_Load(object sender EventArgs e)
{
if (!thisIsPostBack)
{
TreeNode newNode = new TreeNode();
newNodeValue = ;
newNodeText = 我的网站;
newNodePopulateOnDemand = true;
newNodeSelectAction = TreeNodeSelectActionExpand;
TreeViewNodesAdd(newNode);
}
}
protected void TreeView_TreeNodePopulate(object sender TreeNodeEventArgs e)
{
TreeNode newNode;
if (eNode == null)
{
newNode = new TreeNode();
newNodeValue = ;
newNodeText = 开始站点;
newNodePopulateOnDemand = true;
newNodeSelectAction = TreeNodeSelectActionExpand;
TreeViewNodesAdd(newNode);
}
else
{
string sql = select * from Data where pid=+eNodeValue;
SystemDataOleDbOleDbConnection cn = new SystemDataOleDbOleDbConnection();
cnConnectionString = Provider=MicrosoftJetOLEDB;Data Source= + ServerMapPath(~/) + @\App_Data\treemdb;Persist Security Info=True;
cnOpen();
OleDbCommand cmd = new OleDbCommand(sql cn);
OleDbDataReader dr = cmdExecuteReader();
while (drRead())
{
newNode = new TreeNode();
newNodeText = dr[title]ToString();
newNodeValue = dr[id]ToString();
newNodeNavigateUrl = dr[href]ToString();
newNodePopulateOnDemand = true;
newNodeSelectAction = TreeNodeSelectActionExpand;
eNodeChildNodesAdd(newNode);
}
drClose();
drDispose();
cmdDispose();
cnClose();
cnDispose();
cn = null;
}
}
}
SQL动态加载数据几种触发事件
TreeView 服务器控件的事件
TreeNodeCheckChange 当TreeView服务器控件的复选框在向服务器的两次发送过程之间有所更改时发生
SelectedNodeChange 当选择TreeView服务器控件中的节点时发生
TreeNodeExpanded 当展开TreeView服务器控件中的节点时发生
TreeNodeCollapsed 当折叠TreeView服务器控件中的节点时发生
TreeNodePoulate 当其PopulateOnDemand属性设置为true的节点在TreeView服务器控件中展开时发生reeView
TreeNOdeDataBound 当数据项绑定到TreeView服务器控件中的节点时发生
节点的选择
TreeView服务器控件的节点文字有两种模式选择模式和导航模式默认情况下节点的文字处于选择模式
SelectAction属性来指定单击TreeView服务器控件的节点时所触发的事件
None 当选择节点时不触发任何事件
select 当选择节点时TreeView服务器控件触发SelectNodeChange事件
Expand 当选择节点时TreeView服务器控件触发TreeNodeExpanded事件
selectExpand 当选择节点时TreeView服务器控件同时触发SelectNodeChange和TreeNodeEcpand事件
using SystemDataSqlClient;
public partial class Default : SystemWebUIPage
{
protected void Page_Load(object sender EventArgs e)
{
if (!IsPostBack)
{
DataView DView = dv(select * from BCity);
foreach (DataRowView drv in DView)
{
TreeNode tn = new TreeNode();
tnText = drv[CityName]ToString();
tnValue = drv[CityID]ToString();
tnExpanded = false;
TreeViewNodesAdd(tn);
AddchildNode(tn);
}
}
}
private void AddchildNode(TreeNode tn)
{
string id = tnValueToString();
DataView dView = dv(select * from SCity where CityID=+id+);
foreach (DataRowView drv in dView)
{
TreeNode ctn = new TreeNode();
ctnText = drv[CityName]ToString();
ctnValue = drv[CityID]ToString();
ctnNavigateUrl = + drv[CityID]ToString();
tnChildNodesAdd(ctn);
}
}
private DataView dv(string str)
{
string mycon = ConfigurationManagerConnectionStrings[mydbconn]ToString();
SqlConnection con = new SqlConnection(mycon);
SqlDataAdapter da = new SqlDataAdapter(strmycon);
DataSet ds = new DataSet();
daFill(ds tree);
return dsTables[tree]DefaultView;
}
protected void Button_Click(object sender EventArgs e)
{
if (TreeViewCheckedNodesCount > )
{
foreach (TreeNode tn in TreeViewCheckedNodes)
{
if (tnChildNodesCount > )
{
foreach (TreeNode ctn in tnChildNodes)
{
ResponseWrite(ctnText +
);
}
}
else
{
ResponseWrite(tnText +
);
}
}
}
}
protected void TreeView_SelectedNodeChanged(object sender EventArgs e)
{
ResponseWrite(你选择了 + TreeViewSelectedNodeText);
}
protected void TreeView_TreeNodeCollapsed(object sender TreeNodeEventArgs e)
{
ResponseWrite(你折叠了 + eNodeText);
}
protected void TreeView_TreeNodeExpanded(object sender TreeNodeEventArgs e)
{
if (IsPostBack)
{
ResponseWrite(你展开了 + eNodeText);
}
}
}