zlb

Asp.net 中DropDownList下拉列表显示Treeview

用户控件

显示行号 复制代码 这是一段程序代码。
  1. <%@ Control Language="C#" AutoEventWireup="true" CodeBehind="WebUserControl2.ascx.cs" Inherits="MultiDropdownSample.WebUserControl2" %>
    
  2. <script language="javascript" type="text/javascript">
    
  3. //    window.onload = function (ctlId) {
    
  4. //        //CloseTree(ctlId);
    
  5. //        debugger;
    
  6. //        alert("aa");
    
  7. 
    
  8. //    } 
    
  9. //    function OpenTree(ctlId) {
    
  10. //        var obj = document.getElementById(ctlId + "TreeView1");
    
  11. //        obj.style.visibility = "hidden";
    
  12. //        obj.style.height = "0px";
    
  13. //    }
    
  14.     function OpenTree(ctlId) {
    
  15.         var Panel = document.getElementById(ctlId + "PanelTree");
    
  16.         Panel.style.height = "500px";
    
  17.         // alert("kai");
    
  18.         //            var lstBox = document.getElementById(ctlId + "DDList");
    
  19.         //            var div = document.getElementById("divTree");
    
  20.         //            div.style.display = '';
    
  21.         var obj = document.getElementById(ctlId + "TreeView1");
    
  22.         if (obj.style.visibility == "visible") {
    
  23. 
    
  24.             CloseTree(ctlId);
    
  25.         }
    
  26.         else {
    
  27.             obj.style.visibility = "visible";
    
  28.             obj.style.height = "100px";
    
  29.         }
    
  30.     }
    
  31.     function CloseTree(ctlId) {
    
  32.         //debugger;
    
  33.         var obj = document.getElementById(ctlId + "TreeView1");
    
  34.         obj.style.visibility = "hidden";
    
  35.         obj.style.height = "0px";
    
  36. //        if (obj.style.visibility == "visible") {
    
  37. 
    
  38. //            CloseListBox(ctlId);
    
  39. //        }
    
  40. //        else {
    
  41. //            obj.style.visibility = "visible";
    
  42. //            obj.style.height = "100px";
    
  43. //        }
    
  44.     }
    
  45.     function CloseListBox(ctlId) {
    
  46.         var panel = document.getElementById(ctlId + "Panel2");
    
  47.         var tabl = document.getElementById(ctlId + "Table2");
    
  48.         // var lstBox = document.getElementById(ctlId + "DDList");
    
  49.         var lstBox = document.getElementById(ctlId + "PanelTree");
    
  50.         lstBox.style.visibility = "hidden";
    
  51.         lstBox.style.height = "0px";
    
  52.         panel.style.height = tabl.style.height;
    
  53.     }
    
  54. 
    
  55.     function OnTreeNodeChecked(ctlId) {
    
  56. var ele = event.srcElement; 
    
  57. if(ele.type=='checkbox') 
    
  58. { 
    
  59. var childrenDivID = ele.id.replace('CheckBox','Nodes'); 
    
  60. var div = document.getElementById(childrenDivID); 
    
  61. if(div==null)return; 
    
  62. var checkBoxs = div.getElementsByTagName('INPUT'); 
    
  63. for(var i=0;i<checkBoxs.length;i++) 
    
  64. { 
    
  65. if(checkBoxs[i].type=='checkbox') 
    
  66. checkBoxs[i].checked=ele.checked; 
    
  67. }
    
  68. }
    
  69. //点?击÷复′选?框ò时±触¥发¢事?件t
    
  70. var o = window.event.srcElement;
    
  71. if (o.tagName == "INPUT" && o.type == "checkbox") {
    
  72.     __doPostBack("", "");
    
  73. }
    
  74. }
    
  75. </script>
    
  76. <script language="javascript" type="text/javascript">
    
  77.  
    
  78.     //checkbox点?击÷事?件t
    
  79.     function OnCheckEvent(ctlID)
    
  80.     {
    
  81.         var objNode = event.srcElement;
    
  82.         if(objNode.tagName != "INPUT" || objNode.type != "checkbox")
    
  83.             return;
    
  84.         //获?得?当±前°树÷结á点?
    
  85.         var ck_ID = objNode.getAttribute("ID");
    
  86.         var node_ID = ck_ID.substring(0,ck_ID.indexOf("CheckBox")) + "Nodes";
    
  87.         var curTreeNode = document.getElementById(node_ID);
    
  88.         //级?联a选?择?
    
  89.         SetChildCheckBox(curTreeNode,objNode.checked);
    
  90.         SetParentCheckBox(objNode);
    
  91. 
    
  92.         //点?击÷复′选?框ò时±触¥发¢事?件t
    
  93.         var o = window.event.srcElement;
    
  94.         if (o.tagName == "INPUT" && o.type == "checkbox") {
    
  95.             __doPostBack("", "");
    
  96.         }
    
  97.     }
    
  98.     
    
  99.     //子ó结á点?字?符?串?
    
  100.     var childIds = "";
    
  101.     //获?取?子ó结á点?ID数y组é
    
  102.     function GetChildIdArray(parentNode)
    
  103.     {
    
  104.         if (parentNode == null)
    
  105.             return;
    
  106.         var childNodes = parentNode.children;
    
  107.         var count = childNodes.length;
    
  108.         for(var i = 0;i < count;i ++)
    
  109.         {
    
  110.             var tmpNode = childNodes[i];
    
  111.             if(tmpNode.tagName == "INPUT" && tmpNode.type == "checkbox")
    
  112.             {
    
  113.                 childIds = tmpNode.id + ":" + childIds;
    
  114.             }
    
  115.             GetChildIdArray(tmpNode);
    
  116.         }
    
  117.     }
    
  118.     
    
  119.     //设è置?子ó结á点?的?checkbox
    
  120.     function SetChildCheckBox(parentNode,checked)
    
  121.     {
    
  122.         if(parentNode == null)
    
  123.             return;
    
  124.         var childNodes = parentNode.children;
    
  125.         var count = childNodes.length;
    
  126.         for(var i = 0;i < count;i ++)
    
  127.         {
    
  128.             var tmpNode = childNodes[i];
    
  129.             if(tmpNode.tagName == "INPUT" && tmpNode.type == "checkbox")
    
  130.             {
    
  131.                 tmpNode.checked = checked;
    
  132.             }
    
  133.             SetChildCheckBox(tmpNode,checked);
    
  134.         }
    
  135.     }
    
  136.     
    
  137.     //设è置?父?结á点?的?checkbox
    
  138.     function SetParentCheckBox(childNode)
    
  139.     {
    
  140.         if(childNode == null)
    
  141.             return;
    
  142.         var parent = childNode.parentNode;
    
  143.         if(parent == null || parent == "undefined")
    
  144.             return;
    
  145.         do 
    
  146.         {
    
  147.         parent = parent.parentNode;
    
  148.         }
    
  149.         while (parent && parent.tagName != "DIV");
    
  150.         if(parent == "undefined" || parent == null)
    
  151.             return;
    
  152.         var parentId = parent.getAttribute("ID");
    
  153.         var objParent = document.getElementById(parentId);
    
  154.         childIds = "";
    
  155.         GetChildIdArray(objParent);
    
  156.         //判D断?子ó结á点?状′态?
    
  157.         childIds = childIds.substring(0,childIds.length - 1);
    
  158.         var aryChild = childIds.split(":");
    
  159.         var result = false;
    
  160.         //当±子ó结á点?的?checkbox状′态?有D一?个?为atrue,?其?父?结á点?checkbox状′态?即′为atrue,否?则ò为afalse
    
  161.         for(var i in aryChild)
    
  162.         {
    
  163.             var childCk = document.getElementById(aryChild[i]);
    
  164.             if(childCk.checked)
    
  165.                 result = true;
    
  166.         }
    
  167.         parentId = parentId.replace("Nodes","CheckBox");
    
  168.         var parentCk = document.getElementById(parentId);
    
  169.         if(parentCk == null)
    
  170.             return;
    
  171.         if(result)
    
  172.             parentCk.checked = true;
    
  173.         else
    
  174.             parentCk.checked = false;
    
  175.         SetParentCheckBox(parentCk);
    
  176.     }
    
  177.    
    
  178. </script>
    
  179. <%--<asp:panel id="Panel2" Height="1px" runat="server" Width="160px" BackColor="White">--%>
    
  180. <TABLE id="Table2" style="TABLE-LAYOUT: fixed; HEIGHT: 24px" cellSpacing="0" borderColorDark="#cccccc"
    
  181.             cellPadding="0" width="100%" borderColorLight="#7eb3e3" border="1" runat="server">
    
  182.             <TR id="rowDD" style="HEIGHT: 15px" runat="server">
    
  183.                 <TD noWrap>
    
  184.                     <asp:Label id="DDLabel" style="CURSOR: default" runat="server" Width="100%" ToolTip="" Font-Size="Smaller"
    
  185.                         Font-Names="Arial" BorderColor="Transparent" BorderStyle="None" height="15px"></asp:Label></TD>
    
  186.                 <TD id="colDDImage" style="PADDING-RIGHT: 0px; PADDING-LEFT: 0px; PADDING-BOTTOM: 0px; PADDING-TOP: 0px; BACKGROUND-COLOR: #7eb3e3"
    
  187.                     width="20" background="Image/DDImage.bmp" runat="server"></TD>
    
  188.             </TR>
    
  189.         </TABLE>
    
  190.         <asp:Panel ID="PanelTree" runat="server" Width="100%">
    
  191.         <div style="Z-INDEX: 9999; POSITION: absolute"  >
    
  192.            
    
  193.            
    
  194.             <asp:TreeView ID="TreeView1" runat="server" Width="100%"   ShowCheckBoxes="All">
    
  195.             </asp:TreeView>
    
  196.             
    
  197.          
    
  198.             </div>
    
  199.         </asp:Panel>
    
  200.  <%--</asp:panel> --%>
    
  201. 
    

后台cs代码

显示行号 复制代码 这是一段程序代码。
  1. using System;
    
  2. using System.Collections.Generic;
    
  3. using System.Linq;
    
  4. using System.Web;
    
  5. using System.Web.UI;
    
  6. using System.Web.UI.WebControls;
    
  7. using System.Collections;
    
  8. 
    
  9. namespace MultiDropdownSample
    
  10. {
    
  11.     public partial class WebUserControl2 : System.Web.UI.UserControl
    
  12.     {
    
  13.         // protected System.Web.UI.WebControls.TreeView DDList;
    
  14.         private string _SelectedText;
    
  15.         #region  Public properties
    
  16. 
    
  17.         /// <summary>
    
  18.         /// Get and Set the width of the Dropdown
    
  19.         /// </summary>
    
  20.         //public double ListWidth
    
  21.         //{
    
  22.         //    get { return Panel2.Width.Value; }
    
  23.         //    set { Panel2.Width = (Unit)value; }
    
  24.         //}
    
  25. 
    
  26.         /// <summary>
    
  27.         /// Gets arraylist of  selected values 
    
  28.         /// </summary>
    
  29.         public ArrayList SelectedValues
    
  30.         {
    
  31.             get
    
  32.             {
    
  33.                 ArrayList selectedValues = new ArrayList();
    
  34.                 //foreach (System.Web.UI.WebControls.ListItem li in TreeView1.CheckedNodes)
    
  35.                 //{
    
  36.                 //    if (li.Selected)
    
  37.                 //    { selectedValues.Add(li.Value); }
    
  38.                 //}
    
  39.                 foreach (TreeNode item in TreeView1.CheckedNodes)
    
  40.                 {
    
  41.                     if (item.Checked)
    
  42.                     {
    
  43.                         selectedValues.Add(item.Value);
    
  44.                     }
    
  45.                 }
    
  46.                 return selectedValues;
    
  47.             }
    
  48.         }
    
  49. 
    
  50.         /// <summary>
    
  51.         /// Gets arraylist of  selected texts 
    
  52.         /// </summary>
    
  53.         public ArrayList SelectedTexts
    
  54.         {
    
  55.             get
    
  56.             {
    
  57.                 ArrayList selectedTexts = new ArrayList();
    
  58.                 //foreach (System.Web.UI.WebControls.ListItem li in DDList.Items)
    
  59.                 //{
    
  60.                 //    if (li.Selected)
    
  61.                 //    { selectedTexts.Add(li.Text); }
    
  62.                 //}
    
  63.                 foreach (TreeNode item in TreeView1.CheckedNodes)
    
  64.                 {
    
  65.                     if (item.Checked)
    
  66.                     {
    
  67.                         selectedTexts.Add(item.Text);
    
  68.                     }
    
  69.                 }
    
  70.                 return selectedTexts;
    
  71.             }
    
  72.         }
    
  73. 
    
  74.         /// <summary>
    
  75.         /// Gets the selected text , the items are separated by comma
    
  76.         /// </summary>
    
  77.         public string SelectedText
    
  78.         {
    
  79.             get
    
  80.             {
    
  81.                 string selText = string.Empty;
    
  82.                 //foreach (System.Web.UI.WebControls.ListItem li in DDList.Items)
    
  83.                 //{
    
  84.                 //    if (li.Selected)
    
  85.                 //    { selText += li.Text + ","; }
    
  86.                 //}
    
  87.                 foreach (TreeNode item in TreeView1.CheckedNodes)
    
  88.                 {
    
  89.                     if (item.Checked)
    
  90.                     {
    
  91.                         selText += item.Text + ",";
    
  92.                     }
    
  93.                 }
    
  94.                 if (selText.Length > 0)
    
  95.                     selText = selText.Length > 0 ? selText.Substring(0, selText.Length - 1) : selText;
    
  96.                 return selText;
    
  97.             }
    
  98.             set
    
  99.             {
    
  100.                 _SelectedText = value;
    
  101.                 DDLabel.Text = _SelectedText;
    
  102.                 DDLabel.ToolTip = _SelectedText;
    
  103.             }
    
  104.         }
    
  105. 
    
  106.         /// <summary>
    
  107.         /// Gets the selected items of the list
    
  108.         /// </summary>
    
  109.         public ArrayList SelectedItems
    
  110.         {
    
  111.             get
    
  112.             {
    
  113.                 ArrayList selectedItems = new ArrayList();
    
  114.                 //foreach (System.Web.UI.WebControls.ListItem li in DDList.Items)
    
  115.                 //{
    
  116.                 //    if (li.Selected)
    
  117.                 //    { selectedItems.Add(li); }
    
  118.                 //}
    
  119.                 foreach (TreeNode item in TreeView1.CheckedNodes)
    
  120.                 {
    
  121.                     if (item.Checked)
    
  122.                     {
    
  123.                         selectedItems.Add(item);
    
  124.                     }
    
  125.                 }
    
  126.                 return selectedItems;
    
  127.             }
    
  128.             set
    
  129.             {
    
  130.                 ArrayList selectedItems = value;
    
  131.                 string selText = string.Empty;
    
  132. 
    
  133.                 // Deselect all the selected items
    
  134.                 //foreach (System.Web.UI.WebControls.ListItem li in DDList.Items)
    
  135.                 //{ li.Selected = false; }
    
  136.                 foreach (TreeNode item in TreeView1.CheckedNodes)
    
  137.                 {
    
  138.                     item.Checked = false;
    
  139.                 }
    
  140.                 // Select the items from the list
    
  141.                 //foreach (System.Web.UI.WebControls.ListItem selItem in selectedItems)
    
  142.                 //{
    
  143.                 //    System.Web.UI.WebControls.ListItem li = DDList.Items.FindByText(selItem.Text);
    
  144.                 //    if (li != null)
    
  145.                 //    { li.Selected = true; selText += li.Text + ","; }
    
  146.                 //}
    
  147.                 foreach (TreeNode selItem in selectedItems)
    
  148.                 {
    
  149.                     TreeNode node = TreeView1.FindNode(Server.HtmlEncode(selItem.Text.Trim()));
    
  150.                     if (node != null)
    
  151.                     {
    
  152.                         node.Checked = true;
    
  153.                         selText += node.Text + ",";
    
  154.                     }
    
  155.                     //TreeNode item=this.TreeView1.Nodes.IndexOf(..FindNode(.CheckedNodes..Contains(selItem)
    
  156.                 }
    
  157.                 if (selText.Length > 0)
    
  158.                     selText = selText.Length > 0 ? selText.Substring(0, selText.Length - 1) : selText;
    
  159. 
    
  160.                 SelectedText = selText;
    
  161.             }
    
  162.         }
    
  163.         /// <summary>
    
  164.         /// Gets the list
    
  165.         /// </summary>
    
  166.         public System.Web.UI.WebControls.TreeView TreeViewList
    
  167.         {
    
  168.             get { return this.TreeView1; }
    
  169.             set { TreeView1 = TreeViewList; }
    
  170.         }
    
  171.         #endregion
    
  172. 
    
  173.         #region Public methods
    
  174. 
    
  175.         /// <summary>
    
  176.         /// Remove all items in the list 
    
  177.         /// </summary>
    
  178.         public void Clear()
    
  179.         {
    
  180.             this.TreeView1.Nodes.Clear();// DDList.Items.Clear();
    
  181.         }
    
  182.         #endregion
    
  183. 
    
  184.         #region Private methods
    
  185. 
    
  186.         /// <summary>
    
  187.         /// Set the attributes of the controls
    
  188.         /// </summary>
    
  189.         public void PageInit()
    
  190.         {
    
  191.             string ctlID = this.UniqueID + "_";
    
  192.             //TreeView1.Attributes.Add("onchange", "SelectedIndexChanged('" + ctlID + "');");
    
  193.             //TreeView1.Attributes.Add("onmouseout", "CloseListBox('" + ctlID + "');");
    
  194.             //PanelTree.Attributes.Add("onmouseout", "CloseListBox('" + ctlID + "');");
    
  195.             // DDLabel.Attributes.Add("onclick", "OpenListBox('" + ctlID + "');");
    
  196.             // colDDImage.Attributes.Add("onclick", "OpenListBox('" + ctlID + "');");
    
  197.             //SetTreeNodeAutoSelectParentNodeHandle
    
  198.             //TreeView1.Attributes.Add("onclick", "SetTreeNodeAutoSelectParentNodeHandle('" + ctlID + "');"); 
    
  199.             //TreeView1.Attributes.Add("onclick", "OnCheckBoxCheckChanged(event)");   
    
  200.             DDLabel.Attributes.Add("onclick", "OpenTree('" + ctlID + "');");
    
  201.             colDDImage.Attributes.Add("onclick", "OpenTree('" + ctlID + "');");
    
  202.             TreeView1.Attributes.Add("onmouseover", "OpenTree('" + ctlID + "');");
    
  203.             PanelTree.Attributes.Add("onmouseout", "CloseTree('" + ctlID + "');");
    
  204.           
    
  205.             //TreeView1.Attributes.Add("OnClick", "OnTreeNodeChecked('" + ctlID + "');");
    
  206. 
    
  207.             TreeView1.Attributes.Add("onclick", "OnCheckEvent('" + ctlID + "');");
    
  208. 
    
  209.            // this.Attributes.Add("onload", "CloseTree('" + ctlID + "');");
    
  210. 
    
  211.             if (!Page.ClientScript.IsStartupScriptRegistered(this.GetType(), "onloadjs"))
    
  212.                 Page.ClientScript.RegisterStartupScript(this.GetType(), "onloadjs",
    
  213.                   "window.onload = function() { CloseTree('" + ctlID + "');};", true);
    
  214.         }
    
  215. 
    
  216. 
    
  217.         /// <summary>
    
  218.         /// Page load event
    
  219.         /// </summary>
    
  220.         /// <param name="sender"></param>
    
  221.         /// <param name="e"></param>
    
  222.         private void Page_Load(object sender, System.EventArgs e)
    
  223.         {
    
  224.             if (!Page.IsPostBack)
    
  225.             {
    
  226.                 PageInit();
    
  227. 
    
  228.                 TreeView1.Height = 0;
    
  229.                 if (TreeView1.Nodes.Count > 0)
    
  230.                     DDLabel.Text = TreeView1.Nodes[0].Text;
    
  231.                 else
    
  232.                     DDLabel.Text = string.Empty;
    
  233.             }
    
  234.             else
    
  235.             {    // set the selected text and tooltip
    
  236.                 DDLabel.Text = SelectedText;
    
  237.                 DDLabel.ToolTip = SelectedText;
    
  238.             }
    
  239.             string ctlID = this.UniqueID + "_";
    
  240.          
    
  241.         }
    
  242. 
    
  243.         #endregion
    
  244.     }
    
  245. }
    

调用示例

webForm.aspx页面

显示行号 复制代码 这是一段程序代码。
  1. <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm3.aspx.cs" Inherits="MultiDropdownSample.WebForm3" %>
    
  2. <%@ Register TagPrefix="uc1" TagName="WebUserControl2" Src="WebUserControl2.ascx" %>
    
  3. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    
  4. 
    
  5. <html xmlns="http://www.w3.org/1999/xhtml">
    
  6. <head runat="server">
    
  7.     <title></title>
    
  8. </head>
    
  9. <body>
    
  10.     <form id="form1" runat="server"  >
    
  11.     <div height="55px">
    
  12.     <uc1:WebUserControl2 id="MultiSelectDropDown1" runat="server"></uc1:WebUserControl2>
    
  13.     </div>
    
  14.     <asp:DropDownList ID="DropDownList1" runat="server">
    
  15.     </asp:DropDownList>
    
  16.     </form>
    
  17. </body>
    
  18. </html>
    
  19. 
    
  20. 
    

pageload 事件

显示行号 复制代码 这是一段程序代码。
  1. protected void Page_Load(object sender, EventArgs e)
    
  2.      {
    
  3.          if (!Page.IsPostBack)
    
  4.          {
    
  5.              MultiSelectDropDown1.Clear();
    
  6.              //MultiSelectDropDown1.List.Items.Add(new System.Web.UI.WebControls.ListItem("Apple", "1"));
    
  7.              //MultiSelectDropDown1.List.Items.Add(new System.Web.UI.WebControls.ListItem("Grapes", "2"));
    
  8.              //MultiSelectDropDown1.List.Items.Add(new System.Web.UI.WebControls.ListItem("Orange", "3"));
    
  9.              //MultiSelectDropDown1.List.Items.Add(new System.Web.UI.WebControls.ListItem("Strawberry", "4"));
    
  10.              //MultiSelectDropDown1.List.Items.Add(new System.Web.UI.WebControls.ListItem("Water Melon", "5"));
    
  11.              //MultiSelectDropDown1.TreeViewList.Nodes.Add(new TreeNode { Text = "1" });
    
  12.              TreeNode userTools = new TreeNode();
    
  13.              userTools.Text = "User Tools";
    
  14.              MultiSelectDropDown1.TreeViewList.Nodes.Add(userTools);
    
  15. 
    
  16.              TreeNode userTool1 = new TreeNode();
    
  17.              userTool1.Text = "User Tool 1";
    
  18.              MultiSelectDropDown1.TreeViewList.Nodes[MultiSelectDropDown1.TreeViewList.Nodes.IndexOf(userTools)].ChildNodes.Add(userTool1);
    
  19.              // tree.Nodes[0].Nodes.Add(userTool1); // This also works
    
  20. 
    
  21.              TreeNode userTool2 = new TreeNode();
    
  22.              userTool2.Text = "User Tool 2";
    
  23.              MultiSelectDropDown1.TreeViewList.Nodes[MultiSelectDropDown1.TreeViewList.Nodes.IndexOf(userTools)].ChildNodes[userTools.ChildNodes.IndexOf(userTool1)].ChildNodes.Add(userTool2);
    
  24.              // tree.Nodes[0].Nodes[0].Nodes.Add(userTool2); // This also works
    
  25.          }
    
  26.      }
    

下载地址

posted on 2011-03-08 17:06  zlb  阅读(2223)  评论(0编辑  收藏  举报

导航