惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Google DeepMind News
Google DeepMind News
N
Netflix TechBlog - Medium
The Register - Security
The Register - Security
C
Cybersecurity and Infrastructure Security Agency CISA
H
Hackread – Cybersecurity News, Data Breaches, AI and More
The Hacker News
The Hacker News
P
Proofpoint News Feed
Project Zero
Project Zero
The GitHub Blog
The GitHub Blog
The Last Watchdog
The Last Watchdog
F
Fortinet All Blogs
S
Schneier on Security
Help Net Security
Help Net Security
Security Archives - TechRepublic
Security Archives - TechRepublic
C
Check Point Blog
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
P
Proofpoint News Feed
I
InfoQ
T
The Blog of Author Tim Ferriss
Cisco Talos Blog
Cisco Talos Blog
Stack Overflow Blog
Stack Overflow Blog
T
Troy Hunt's Blog
人人都是产品经理
人人都是产品经理
T
Threatpost
www.infosecurity-magazine.com
www.infosecurity-magazine.com
C
Cyber Attacks, Cyber Crime and Cyber Security
雷峰网
雷峰网
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
爱范儿
爱范儿
Forbes - Security
Forbes - Security
Vercel News
Vercel News
S
Security Affairs
美团技术团队
P
Privacy & Cybersecurity Law Blog
N
News and Events Feed by Topic
Cyberwarzone
Cyberwarzone
Recent Commits to openclaw:main
Recent Commits to openclaw:main
Jina AI
Jina AI
Spread Privacy
Spread Privacy
Attack and Defense Labs
Attack and Defense Labs
IT之家
IT之家
U
Unit 42
Recorded Future
Recorded Future
W
WeLiveSecurity
PCI Perspectives
PCI Perspectives
P
Palo Alto Networks Blog
H
Hacker News: Front Page
S
Security @ Cisco Blogs
博客园 - 【当耐特】

博客园 - 思然

邮件带图片附件 - 思然 - 博客园 XML查找节点 - 思然 - 博客园 动态生成rdlc 报表(原创) 图片处理 - 思然 - 博客园 数据绑定及其他 - 思然 - 博客园 图片上传及网络相册功能 SQL Server 2005 Express 使用心得 - 思然 附加进程调试和存储过程调试 (转)DIV CSS布局教程:应用ul、li实现表格形式 用户控件的使用经验 缓存之缓存文件依赖及编程方式设置输出缓存过期 防刷新多次提交 枚举的应用 添加div符号注意符号问题 Datalist调用本地文件绑定图片 关于table控件的一个疑难问题(涉及循环) 关于javascript调用webservices的中文参数乱码的问题 服务器端和客户端清除TextBox控件的值 获取母版页的控件的方法
用户控件-TreeView的用法
思然 · 2008-01-04 · via 博客园 - 思然

Treeview中ontreenodepopulate是一个很好的事件,当你点击树的某个节点时调入数据库展现此节点下的数据,并且无刷新,当树展现的数据多的时候就可以用此方法如MSDN的树,但如果数据不多的时候没有必要用此方法,因为要频繁调数据库
好闲话少说
由于是用户控件所以把 treeview的部分属性和事件公共,方便用此控件的页面调用
页面 TreeDept.ascx
<%@ Control Language="C#" AutoEventWireup="true" CodeFile="TreeDept.ascx.cs" Inherits="UserControls_TreeDept" %>
<asp:TreeView ID="TreeView1" runat="server" ExpandDepth="1" ImageSet="Contacts"
    NodeIndent="10" onselectednodechanged="TreeView1_SelectedNodeChanged"
    ontreenodepopulate="TreeView1_TreeNodePopulate" Width="101px">
    <ParentNodeStyle Font-Bold="True" ForeColor="#5555DD" />
    <HoverNodeStyle Font-Underline="False" />
    <SelectedNodeStyle Font-Underline="True" HorizontalPadding="0px"
        VerticalPadding="0px" />
    <NodeStyle Font-Names="Verdana" Font-Size="8pt" ForeColor="Black"
        HorizontalPadding="5px" NodeSpacing="0px" VerticalPadding="0px" />
</asp:TreeView>
TreeDept.ascx.CS

public partial class UserControls_TreeDept : System.Web.UI.UserControl
{
  
    public event EventHandler DeptSel;
    public string DeptSelectNodeText
    {
        get { return TreeView1.SelectedNode.Text; }
    }
    SqlConnection con = new SqlConnection(@"server=LT-C9A4FDA64461;uid=sa;pwd=sa;database=ltweb");
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            BindTree();
        }
    }
    void BindTree()
    {
        TreeNode tn = new TreeNode();
        tn.Text = "软件";
        tn.Value = "1";
        tn.PopulateOnDemand = true;
        TreeView1.Nodes.Add(tn);

    }

    protected void TreeView1_TreeNodePopulate(object sender, TreeNodeEventArgs e)
    {
        SqlCommand cmd = new SqlCommand("select * from dept where parentID="+int.Parse(e.Node.Value),con);
        SqlDataAdapter sda = new SqlDataAdapter(cmd);
        DataTable dt = new DataTable();
        sda.Fill(dt);
        foreach (DataRow dr in dt.Rows)
        {
            TreeNode tn = new TreeNode(dr["Dept_Name"].ToString(), dr["Dept_ID"].ToString());
            tn.PopulateOnDemand = true;
            e.Node.ChildNodes.Add(tn);

        }
    }
    protected void TreeView1_SelectedNodeChanged(object sender, EventArgs e)
    {
        if(DeptSel!=null)
        DeptSel(this,EventArgs.Empty);
    }
}

调用页面
 <form id="form1" runat="server">
    <div>
   
        <uc1:TreeDept ID="TreeDept1" runat="server" OnDeptSel="DeptSel_Change"/>
   
    </div>
    </form>

后台

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

    }
    protected void DeptSel_Change(object sender, EventArgs e)
    {
        Response.Write(TreeDept1.DeptSelectNodeText);
    }
}