• TreeView的选中CheckBox触发事件


    前台:

    <html xmlns="http://www.w3.org/1999/xhtml" >

    <head runat="server">

        <title>TreeView选择checkBox时触发函数</title>

        <script language="javascript" type="text/javascript">

    // 点击复选框时触发事件

    function postBackByObject()

    {

          var o = window.event.srcElement;

          if (o.tagName == "INPUT" && o.type == "checkbox")

          {

             __doPostBack("","");

          }

       }

    </script>

    </head>

    <body>

        <form id="form1" runat="server">

        <div>

            <asp:TreeView ID="TreeView1" runat="server" OnTreeNodeCheckChanged="TreeView1_TreeNodeCheckChanged">

            </asp:TreeView>

            &nbsp;</div>

        </form>

    </body>

    </html>

    后台:

    protected void Page_Load(object sender, EventArgs e)

        {

            if (!IsPostBack)

            {

                //构建TreeView

                TreeNode Node00 = new TreeNode("00", "00");

                TreeNode Node01 = new TreeNode("01", "01");

                TreeNode Node0101 = new TreeNode("0101", "0101");

                TreeNode Node0102 = new TreeNode("0102", "0102");

                TreeNode Node02 = new TreeNode("02", "02");

                Node01.ChildNodes.Add(Node0101);

                Node01.ChildNodes.Add(Node0102);

                Node00.ChildNodes.Add(Node01);

                Node00.ChildNodes.Add(Node02);

                TreeView1.Nodes.Add(Node00);

                TreeView1.ShowCheckBoxes = TreeNodeTypes.All;//显示CheckBox

                TreeView1.ExpandDepth = 0;

            }

            TreeView1.Attributes.Add("onclick", "postBackByObject()");

        }

        protected void TreeView1_TreeNodeCheckChanged(object sender, TreeNodeEventArgs e)

        {

            SetChildChecked(e.Node);

        }

        private void SetChildChecked(TreeNode parentNode)

        {

            foreach (TreeNode node in parentNode.ChildNodes)

            {

                node.Checked = parentNode.Checked;

                if (node.ChildNodes.Count > 0)

                {

                    SetChildChecked(node);

                }

            }

        }

    这里客户端有个 __doPostBack函数,弄清它的原理,如下:

    __doPostBack(id, eventArgument)来和控件交互。doPostBack的用法是,id: 服务器控件的ClientID,eventArgument: 传回服务器的参数。这个参数在服务器端通过IPostBackDataHandler.LoadPostData(string postDataKey, NameValueCollection postCollection)来读取

    了解到此

  • 相关阅读:
    Sharepoint2013搜索学习笔记之创建搜索服务(二)
    Sharepoint2013搜索学习笔记之设置外网内容源(四)
    C# zxing插件 根据输入的字符串生成二维码
    值类型和引用类型
    Web.config增删查改
    Redis
    2017年最好的6个WEB前端开发手册下载
    php implode()函数详解
    elk日志系统搭建
    aop实现接口请求参数打印
  • 原文地址:https://www.cnblogs.com/liufei88866/p/1918745.html
Copyright © 2020-2023  润新知